Android系统的4种Service详解

Android的Service一直都是蒙在心里,Service究竟是什么?其实Service和Activity是对立的东西,Activity在Android系统中最要的就是跟用户交互,但是Service是不用交互的,主要就是长期在后台运行(重点:它默认并不会运行在子线程中,也不是独立运行在独立进程中,同样还是运行在UI线程里面,同样也不能做耗时的操作)。


了解Service一定要一定要理解他们的生命周期

说到生命周期,一开始我们都不知都干嘛用,为什么要有生命周期的这个东西?设计生命周期的目的是满足了面向对象设计。

======onCreate:
=====onStartCommand:
=====onDestroy:

其实服务的生命周期很简单,只有三生命周期方法个onCreateonStartCommandonDestroy


启动一个服务,当我们调用

Intent intent = new Intent(mContext, MyService.class);
startService(intent);

启动一个服务的时候,会执行onCreateonStartCommand这两个方法,但是我们再次调用上面的代码的时候,你会发现不会走onCreate的方法了,只会走onStartCommand这个方法了。细心的朋友会发现,在Android中很多情况比如创建一个数据库,都是只会走一次onCreate


服务启动了,那我们怎么暂停服务呢?
只需要调用在服务里面调用stopSelf()stopService()这个两个方法就可以把服务给关闭了。

stopService(new Intent(mContext,MyService.class));

上面我们已经提到了,服务也是运行在UI线程里面的,所以不能执行耗时的操作,否则会出现ANR。所以服务通常有一套模板

public class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        LogUtil.log("======onCreate:");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LogUtil.log("======onStartCommand:");
        doSomething();
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 由于服务不能执行耗时的操作,所以这里要启动一个行程执行耗时操作
     */
    private void doSomething() {
        new Thread(new ServiceThread()).start();
    }

    @Override
    public void onDestroy() {
        LogUtil.log("======onDestroy:");
        super.onDestroy();
    }
}

然后Google早已经了解一切了,假如你要启动一个服务就是为了做一下简单的操作,做完就立即关闭这个服务还有调用stopSelf()stopService()两个方法,那不是很麻烦?其实Google已经已经提供了另一种服务IntentService。

public class MyIntentService extends IntentService {

    public MyIntentService() {
        // 调用父类的有参构造函数
        super("MyIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // 执行耗时操作
        LogUtil.log("======执行耗时操作完毕");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.log("======服务已经停止");
    }
}

是不是很简单,只要启动它,然后做耗时的操作就行了,这个服务就会自动的关闭。启动方式和startService的方式一样


然后服务并没有那么简单,你是不是觉得少了一些东西,那就是跟Service进行交互了,这不得说起服务的另一种启动方式,通过bindService的方式进行启动。

public class MyBinderService extends Service {
    //
    private DownloadBinder mBinder = new DownloadBinder();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        LogUtil.log("===Binder===onBind");
        return mBinder;
    }


    @Override
    public void onCreate() {
        LogUtil.log("======Binder=====onCreate");
        super.onCreate();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        LogUtil.log("======Binder=====onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        LogUtil.log("======Binder=====onDestroy");
        super.onDestroy();
    }
}
else if (mBtnStartBinder.getId() == v.getId()) {
            if (myServiceConnection==null){
                myServiceConnection = new MyServiceConnection();
            }
            Intent intent = new Intent(mContext, MyBinderService.class);
            bindService(intent, myServiceConnection, BIND_AUTO_CREATE);
        } else if (mBtnStopBinder.getId() == v.getId()) {
            if (myServiceConnection != null) {
                unbindService(myServiceConnection);
                myServiceConnection = null;
            }
        }

bindService只会启动一次,无论你调多少次bindService,不会多次启动最后:所有的Service都需要在清单文件中注册,服务有,startService(3个生命周期方法:onCreate ==> onStartCommand ==>onDestroy),bindService(4个生命周期方法:onCreate==>onBind–onUnbind==>onDestroy),IntentService,前台服务(类似于天气预报属于startService的一种),一共四种!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值