Android| |Service

1、Service是什么?

  • Service是一个应用组件
  • Service没有图形化界面
  • Service通常通常用来处理耗时比较长的操作,如下载、播放
  • 可以使用Service更新Content Provider,发送Intent以及启动系统通知等等

对比Activity,是不可见的,在后台运行的,通常用来处理相对耗时长的操作

对于BroadcastReceiver,如果onReceiver中执行耗时太长,一般选用Service

2、Service不是什么?

Service不是一个单独的进程

Servic不是一个线程

3、Service生命周期

我们通过一个简单的例子来阐述它的周期:

两个按钮,点击绑定和解除

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="chao.pers.testbc2.TestBC2Activity">

   <Button
       android:text="绑定监听器"
       android:id="@+id/register"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" />
    <Button
        android:text="解除监听器绑定"
        android:id="@+id/unregister"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

  继承Service类的FirstService

public class FirstService extends Service{
    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("Service onBlind");
        return null;
    }

    //当创建一个Service对象之后,会调用这个方法
    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("Service onCreate");
    }

    /**
     *
     * @param intent 可以通过intent来向Service传递数据
     * @param flags
     * @param startId
     * @return
     */
    @Override
    //进入到这个方法当中,通常会启动一些线程来操作intent任务
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("flags-->"+flags);
        System.out.println("startId-->"+startId);
        System.out.println("Service onStartCommand");
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        System.out.println("Service onDestory");
        super.onDestroy();
    }
}

  当然我们还需要在Manifest.xml文件中注册该Service

<service android:name=".FirstService"/>

布局很简单,就是两个按钮,分别绑定事件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="chao.pers.testservice1.TestActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    <Button
        android:text="StartService"
        android:id="@+id/startService"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Button
        android:text="StopService"
        android:id="@+id/stopService"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

我们直接点击两次STARTSERVICE

发现只调用了一次onCreate方法

点击STOPSERVICE

4、启动和停止Service的方法

启动Service:Context.startService();

停止Service:Context.stopService();

Activity是Context的子类

当第一次调用startService方法的时候,会调用Service类中的onCreate方法和onStartCommand方法,当再次调用startService方法的时候,不再会去调用onCreate方法,而是直接调用onStartCommand方法,当调用stopService方法的时候,会调用onDestroy方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值