Android之Service自启动流程

 当Service被异常销毁后,默认都会自动重启,但Service的自动重启是如何实现的尼?下面就来梳理一下Service的自动重启流程。
 无论Service是启动还是绑定成功,都会调用ActiveServicesserviceDoneExecutingLocked方法,来看一下这个方法的实现。

    void serviceDoneExecutingLocked(ServiceRecord r, int type, int startId, int res) {
   
        boolean inDestroying = mDestroyingServices.contains(r);
        if (r != null) {
   
            if (type == ActivityThread.SERVICE_DONE_EXECUTING_START) {
   
                // This is a call from a service start...  take care of
                // book-keeping.
                //默认调用Service的startCommand这个方法
                r.callStart = true;
                switch (res) {
   
                    case Service.START_STICKY_COMPATIBILITY:
                    case Service.START_STICKY: {
   
                        // We are done with the associated start arguments.
                        r.findDeliveredStart(startId, true);
                        // Don't stop if killed.
                        //被异常kill掉,允许重启该Service
                        r.stopIfKilled = false;
                        break;
                    }
                    case Service.START_NOT_STICKY: {
   
                        // We are done with the associated start arguments.
                        r.findDeliveredStart(startId, true);
                        if (r.getLastStartId() == startId) {
   
                            // There is no more work, and this service
                            // doesn't want to hang around if killed.
                            //被异常kill掉,不允许重启该Service
                            r.stopIfKilled = true;
                        }
                        break;
                    }
                    case Service.START_REDELIVER_INTENT: {
   
                        // We'll keep this item until they explicitly
                        // call stop for it, but keep track of the fact
                        // that it was delivered.
                        ServiceRecord.StartItem si = r.findDeliveredStart(startId, false);
                        if (si != null) {
   
                            si.deliveryCount = 0;
                            si.doneExecutingCount++;
                            // Don't stop if killed.
                            //被异常kill掉,不允许重启。但后面会依靠deliveredStarts触发重启
                            r.C= true;
                        }
                        break;
                    }
                    case Service.START_TASK_REMOVED_COMPLETE: {
   
                        // Special processing for onTaskRemoved().  Don't
                        // impact normal onStartCommand() processing.
                        r.findDeliveredStart(startId, true);
                        break;
                    }
                    default:
                        throw new IllegalArgumentException(
                                "Unknown service start result: " + res
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值