Service 启动流程分析

之前一篇介绍了《Activity 启动流程分析》,本篇继续学习 Android中Service的启动流程。

Service的启动方式

有两种,分别是startService(),bindService(),经历的生命周期也不太一样。

  1. 通过 startService()
    onCreate() -> onStartCommand() -> onDestroy()
    建议不要在onStartCommand()做耗时操作

  2. 通过 bindService()
    onCreate() -> onbind() -> onUnbind()-> onDestroy()

Service的所有生命周期方法都是在主线程进行的,即ActivityThread启动的线程

onStartCommand()返回值种类:

  • START_NOT_STICKY
  • START_STRCKY
  • START_REDELIVER_INTENT

Service以及四大组件多进程?

android的四大组件都可以开启多进程,通过manifest文件设置process属性

可以同时startService()和bindService()时?如何退出?

答案是可以的,参考 https://blog.csdn.net/qq_22804827/article/details/78609636

  1. service的onCreate只会执行一次,onBind也只会执行一次,onStartCommand可以执行多次
  2. 无论多少个Activity绑定了Service,onBind()和onUnBind()只会执行一次
  3. 多个Activity绑定Service,只有所有的Activity与Service绑定的Contxet失效后,Service才会执行onUnbind(),最后onDestory()进行销毁
  4. 多个Activity绑定Service,只有所有的Activity调用stopService(),或者在Service内部调用stopSelf(),最后才会停止服务。
  5. 同时startService()和bindService(),只有stopService() 加上unBindService()让context失效,最后才会停止服务。

涉及的类

  1. ContextImpl:Context的具体实现
  2. ActivityManagerService:四大组件的服务管理,负责分发任务
  3. ActiveServices:实际负责Service启动绑定等事情
  4. ApplicationThread:表示APP的主线程,有main函数
  5. ActivityThread:ActivityThread的内部类,是一个binder对象,是ActivityManagerService向ActivityThread通信的桥梁
  6. LoadedApk:

StartService()

描述
//ContextWrapper
public ComponentName startService(Intent service) {
    return mBase.startService(service);
}

我们通过Context.startService(),走到ContextWrapper,mBase是一个Context,具体实现在ContextImpl

// ContextImpl
public ComponentName startService(Intent service) {
   warnIfCallingFromSystemProcess();
   return startServiceCommon(service, false, mUser);
}

private ComponentName startServiceCommon(Intent service, boolean requireForeground,
            UserHandle user) {
    try {
        validateServiceIntent(service);
        service.prepareToLeaveProcess(this);
        ComponentName cn = ActivityManager.getService().startService(
            mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
                        getContentResolver()), requireForeground,
                        getOpPackageName(), user.getIdentifier());
        ...
        return cn;
    }
}
  1. validateServiceIntent() 是验证Intent是否合法
  2. AMS运行在单独的进程,是一个binder,ActivityManager.getService() 获取得到是IActivityManager,它是binder引用,供外部跨进程调用。
// ContextImpl
private void validateServiceIntent(Intent service) {
    if (service.getComponent() == null && service.getPackage() == null) {
        if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
            IllegalArgumentException ex = new IllegalArgumentException(
                    "Service Intent must be explicit: " + service);
            throw ex;
        } else {
            Log.w(TAG, "Implicit intents with startService are not safe: " + service
                    + " " + Debug.getCallers(2, 3));
        }
    }
}

从上面可以看出,service.getComponent() == null && service.getPackage() == null 说明是隐式启动,5.0以后不支持隐式启动。

接着看AMS如何启动service

// ActivityManagerService
public ComponentName startService(IApplicationThread caller, Intent service,
        String resolvedType, boolean requireForeground, String callingPackage, int userId)
        throws TransactionTooLargeException {
   ...
   synchronized(this) {
        final int callingPid = Binder.getCallingPid();
        final int callingUid = Binder.getCallingUid();
        final long origId = Binder.clearCallingIdentity();
        ComponentName res;
        try {
            res = mServices.startServiceLocked(caller, service,
                    resolvedType, callingPid, callingUid,
                    requireForeground, callingPackage, userId);
        } finally {
            Binder.restoreCallingIdentity(origId);
        }
        return res;
    }
}

其中 mServices 是 ActiveServices,他负责管理Service的活动,其实AMS就像一个桥梁,由他接收消息,分派任务给指定的类负责,比如Activity启动就分发给ActivityStarter,Service的启动就分发ActiveServices。

// ActiveServices
 ComponentName startServiceLocked(IApplicationThread caller, Intent service, String resolvedType,
        int callingPid, int callingUid, boolean fgRequired, String callingPackage, final int userId)
        throws TransactionTooLargeException {     
     ServiceLookupResult res =
     retrieveServiceLocked(service, resolvedType, callingPackage,
                    callingPid, callingUid, userId, true, callerFg, false, false);
    // retrieveServiceLocked()获取或者新建一个ServiceRecord
     final boolean bgLaunch = !mAm.isUidActiveLocked(r.appInfo.uid);
    // 调用了startServiceInnerLocked
    ComponentName cmp = startServiceInnerLocked(smap, service, r, callerFg, addToStarting);
}

ComponentName startServiceInnerLocked(ServiceMap smap, Intent service, ServiceRecord r,
        boolean callerFg, boolean addToStarting) throws TransactionTooLargeException {
    ServiceState stracker = r.getTracker();
    if (stracker != null) {
        stracker.setStarted(true, mAm.mProcessStats.getMemFactorLocked(), r.lastActivity);
    }
    r.callStart = false;
    synchronized (r.stats.getBatteryStats()) {
        r.stats.startRunningLocked();
    }
    String error = bringUpServiceLocked(r, service.getFlags(), callerFg, false, false);
    return r.name;
}

bringUpServiceLocked()是一个很重要的方法,startService()、bindService()都会走到。

// ActiveServices
private String bringUpServiceLocked(ServiceRecord r, int intentFlags, boolean execInFg,
        boolean whileRestarting, boolean permissionsReviewRequired)
        throws TransactionTooLargeException {
            
    if (r.app != null && r.app.thread != null) {
        // Service在运行
        sendServiceArgsLocked(r, execInFg, false);
        return null;
    }
    ProcessRecord app;// 获取对应的进程ProcessRecord
    if (!isolated) {
       app = mAm.getProcessRecordLocked(procName, r.appInfo.uid, false);
       if (app != null && app.thread != null) {
               // 将其加入到mPendingServices队列
               app.addPackage(r.appInfo.packageName, r.appInfo.longVersionCode, mAm.mProcessStats);
               // 真正启动Service
               realStartServiceLocked(r, app, execInFg);
       }
     }
     // 如果app进程不存在,则会AMS startProcessLocked()
     if (app == null && !permissionsReviewRequired) {
         if ((app=mAm.startProcessLocked(procName, r.appInfo, true, intentFlags,
                    hostingType, r.name, false, isolated, false)) == null) {
             bringDownServiceLocked(r);
             return msg;
         }
         if (isolated) {
             r.isolatedProc = app;
         }
     }  
}

如果Service目标进程不存在会走AMS startProcessLocked(),跟Activity开启进程一样,Process.start创建一个新的进程,走到ActivityThread的main函数,创建Application等。

// AcitveServices
private final void realStartServiceLocked(ServiceRecord r,
        ProcessRecord app, boolean execInFg) throws RemoteException {
    app.thread.scheduleCreateService(r, r.serviceInfo,
        mAm.compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo), 
        app.repProcState);
    
    requestServiceBindingsLocked(r, execInFg);
    sendServiceArgsLocked(r, execInFg, true);
}

app.thread其实就是IApplicationThread,它是一个binder,对应的服务端就是ApplicationThread,它是ActivityThread的内部类,对外提供跟ActivityThread通信的接口。

最后会走到ActivityThread.scheduleCreateService()

requestServiceBindingsLocked()跟bindService()有关,这里先不管

sendServiceArgsLocked()最后会走到onStartCommand()

// ActivityThread
public final void scheduleCreateService(IBinder token,
        ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
    updateProcessState(processState, false);
    CreateServiceData s = new CreateServiceData();
    s.token = token;
    s.info = info;
    s.compatInfo = compatInfo;
    sendMessage(H.CREATE_SERVICE, s);
}

sendMessage(H.CREATE_SERVICE, s); 就是切换到主线程,因为scheduleCreateService运行在binder线程池中,最后通过H handler处理

// ActivityThread
private void handleCreateService(CreateServiceData data) {
        unscheduleGcIdler();
    LoadedApk packageInfo = getPackageInfoNoCheck(
                data.info.applicationInfo, data.compatInfo);
    Service service = null;
    try {
        // 通过classLoader加载初始化Service
        java.lang.ClassLoader cl = packageInfo.getClassLoader();
        service = packageInfo.getAppFactory()
                .instantiateService(cl, data.info.name, data.intent);
    } 
    try {
        // 创建context
        ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
        context.setOuterContext(service);
        // 如果Application不存在,会创建Application
        Application app = packageInfo.makeApplication(false, mInstrumentation);
        service.attach(context, this, data.info.name, data.token, app,
                ActivityManager.getService());
        // 调用service的onCreate()
        service.onCreate();
        mServices.put(data.token, service);
        try {
            ActivityManager.getService().serviceDoneExecuting(
                    data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
        } 
    }
}

handleCreateService()负责实例化service,创建Context、Application等,跟Activity的hanleLaunchActivity()很相似。最后调用Service的onCreate()

再看下onStartCommand()再哪里被调用?

// ActiveService
private final void sendServiceArgsLocked(ServiceRecord r, boolean execInFg,
    boolean oomAdjusted) throws TransactionTooLargeException {
    r.app.thread.scheduleServiceArgs(r, slice);
}

同样利用ApplicationThread到达AcitivityThread所在的进程。

//ActivityThread
 public final void scheduleServiceArgs(IBinder token, ParceledListSlice args) {
    List<ServiceStartArgs> list = args.getList();
    for (int i = 0; i < list.size(); i++) {
        ServiceStartArgs ssa = list.get(i);
        ServiceArgsData s = new ServiceArgsData();
        s.token = token;
        s.taskRemoved = ssa.taskRemoved;
        s.startId = ssa.startId;
        s.flags = ssa.flags;
        s.args = ssa.args;
        sendMessage(H.SERVICE_ARGS, s);
    }
 }

 private void handleServiceArgs(ServiceArgsData data) {
    Service s = mServices.get(data.token);
    if (s != null) {
        try {
            ...
            int res;
            if (!data.taskRemoved) {
                res = s.onStartCommand(data.args, data.flags, data.startId);
            } else {
                s.onTaskRemoved(data.args);
                res = Service.START_TASK_REMOVED_COMPLETE;
            }
        } 
    }
}

最后切换到ActivityThread的主线程,调用onStartCommand()

bindService()

图片

Service的bindService()跟startService()的流程很类似,但是他涉及到ServiceConnection的传输,还要通过ServiceConnection返回binder的代理给客户端,通知已经绑定成功

public boolean bindService(Intent service, ServiceConnection conn, int flags) {
    return mBase.bindService(service, conn, flags);
    }

// ContextImpl
public boolean bindService(Intent service, ServiceConnection conn, int flags) {
    warnIfCallingFromSystemProcess();
    return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), getUser());
}

private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
           handler, UserHandle user) {
    // 转化成IServiceConnection
    if (mPackageInfo != null) {
        sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
    }
    validateServiceIntent(service);
    try {
         IBinder token = getActivityToken();
          ...
          int res = ActivityManager.getService().bindService(
                mMainThread.getApplicationThread(), getActivityToken(), service,
                service.resolveTypeIfNeeded(getContentResolver()),
                sd, flags, getOpPackageName(), user.getIdentifier());

        return res != 0;
    } 
}

bindServiceCommon有两个主要的任务,

  1. 生成一个ServiceConnection的binder服务,这样当Service绑定成功后就可以通过binder引用的接口调用ServiceConnection的onServiceConnected()。
  2. 将ServiceConnection的对应binder的引用传给AMS,并bindService()

其中binder服务就是LoadApk.ServiceDispatcher,所以mPackageInfo.getServiceDispatcher()负责创建LoadApk.ServiceDispatcher,并提供binder引用。

其实在同一进程内提供的binder实体,跨进程之后会通过binder驱动转化为binder引用,这里通通叫binder,具体可以看Android的binder知识点

// LoadedApk
public final IServiceConnection getServiceDispatcher(ServiceConnection c,
            Context context, Handler handler, int flags) {
    synchronized (mServices) {
        LoadedApk.ServiceDispatcher sd = null;
        ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher> map = mServices.get(context);
        if (map != null) {
                sd = map.get(c);
        }
        if (sd == null) {
                sd = new ServiceDispatcher(c, context, handler, flags);
                if (map == null) {
                    map = new ArrayMap<>();
                    mServices.put(context, map);
                }
                map.put(c, sd);
            } else {
                sd.validate(context, handler);
            }
            return sd.getIServiceConnection();
        }
    }

getServiceDispatcher()先查询是否已经存在已有的ServiceConnection对应的LoadedApk.ServiceDispatcher,如果有就直接复用,不需要再建立LoadedApk.ServiceDispatcher,增加多一个binder了。否则就创建ServiceDispatcher。

//AMS
public int bindService(IApplicationThread caller, IBinder token, Intent service,
        String resolvedType, IServiceConnection connection, int flags, String callingPackage,
        int userId) throws TransactionTooLargeException {
    synchronized(this) {
        return mServices.bindServiceLocked(caller, token, service,
                resolvedType, connection, flags, callingPackage, userId);
    }
}

终于来到了AMS的bindService(),他还是分发给了ActiveServices负责bindServiceLocked()

// ActiveServices
 int bindServiceLocked(IApplicationThread caller, IBinder token, Intent service,
        String resolvedType, final IServiceConnection connection, int flags,
        String callingPackage, final int userId) throws TransactionTooLargeException {
    // 获取或创建一个ServiceRecord
    ServiceLookupResult res =
        retrieveServiceLocked(service, resolvedType, callingPackage, Binder.getCallingPid(),
            Binder.getCallingUid(), userId, true, callerFg, isBindExternal, allowInstant);
    if ((flags&Context.BIND_AUTO_CREATE) != 0) {
         s.lastActivity = SystemClock.uptimeMillis();
         if (bringUpServiceLocked(s, service.getFlags(), callerFg, false,
                permissionsReviewRequired) != null) {
           return 0;
         }
    }
}

// ActiveServices
 private String bringUpServiceLocked(ServiceRecord r, int intentFlags, boolean execInFg,
       boolean whileRestarting, boolean permissionsReviewRequired)
       throws TransactionTooLargeException {

    if (r.app != null && r.app.thread != null) {
       sendServiceArgsLocked(r, execInFg, false);
       return null;
   }
   app = mAm.getProcessRecordLocked(procName, r.appInfo.uid, false);
   if (app != null && app.thread != null) {
       try {
           app.addPackage(r.appInfo.packageName, r.appInfo.longVersionCode, mAm.mProcessStats);
           realStartServiceLocked(r, app, execInFg);
           return null;
       } 
   }
}

在ActiveServices的部分流程上跟startService()没有什么大的不同,同样走到realStartServiceLocked()

// AcitveServices
private final void realStartServiceLocked(ServiceRecord r,
        ProcessRecord app, boolean execInFg) throws RemoteException {
    app.thread.scheduleCreateService(r, r.serviceInfo,
        mAm.compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo),
        app.repProcState);

    requestServiceBindingsLocked(r, execInFg);
    sendServiceArgsLocked(r, execInFg, true);
}

app.thread.scheduleCreateService()负责创建service,不在分析,这里讲上面没说到的requestServiceBindingsLocked()方法,最后怎么调用到了ServiceConnection的onServiceConnected()

// AcitveServices
 private final void requestServiceBindingsLocked(ServiceRecord r, boolean execInFg)
        throws TransactionTooLargeException {
    for (int i=r.bindings.size()-1; i>=0; i--) {
        IntentBindRecord ibr = r.bindings.valueAt(i);
        if (!requestServiceBindingLocked(r, ibr, execInFg, false)) {
            break;
        }
    }
}

private final boolean requestServiceBindingLocked(ServiceRecord r, IntentBindRecord i,
        boolean execInFg, boolean rebind) throws TransactionTooLargeException {
    r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind,
        r.app.repProcState);
}

利用IApplicationThread切换到ActivityThread,最后切换到主线程,调用handleBindService()。这部分基本上是一样的。

// ActivityThread
private void handleBindService(BindServiceData data) {
    Service s = mServices.get(data.token);
    if (s != null) {
    try {
        data.intent.setExtrasClassLoader(s.getClassLoader());
        data.intent.prepareToEnterProcess();
        try {
            if (!data.rebind) {
                // 调用了onBind(),返回一个IBinder对象
                IBinder binder = s.onBind(data.intent);
                ActivityManager.getService().publishService(
                    data.token, data.intent, binder);
            } else {
                s.onRebind(data.intent);
                ActivityManager.getService().serviceDoneExecuting(
                data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
                }
                ensureJitEnabled();
            } 
        } 
    }
}

IBinder binder = s.onBind(data.intent);
调用了Service的生命周期onBind(),还返回了一个binder(这里应该是binder实体,因为同一个进程内),本来开始以为这里会利用LoadedApk.ServiceDispatcher的binder引用调用ServiceConnection的onServiceConnected(),实际上并没有,而是又跨进程调用AMS的pubishService,由AMS回调ServiceConnection的onServiceConnected()

// AMS
public void publishService(IBinder token, Intent intent, IBinder service) {
    synchronized(this) {
        mServices.publishServiceLocked((ServiceRecord)token, intent, service);
    }
}

这里老规矩,AMS分发任务给ActiveServices

// ActiveServices
void publishServiceLocked(ServiceRecord r, Intent intent, IBinder service) {
    final long origId = Binder.clearCallingIdentity();
    if (r != null) {
        Intent.FilterComparison filter = new Intent.FilterComparison(intent);
        IntentBindRecord b = r.bindings.get(filter);
        if (b != null && !b.received) {
            b.binder = service;
            b.requested = true;
            b.received = true;
            for (int conni=r.connections.size()-1; conni>=0; conni--) {
                ArrayList<ConnectionRecord> clist = r.connections.valueAt(conni);
                for (int i=0; i<clist.size(); i++) {
                    ConnectionRecord c = clist.get(i);
                    if (!filter.equals(c.binding.intent.intent)) {
                        continue;
                    }
                    ...
                    c.conn.connected(r.name, service, false);
                }
             }
             ...
         }
     } 
}

这里知道为什么了ActivityThread没有直接跨进程调用ServiceConnection的onServiceConnected(),原来只有AMS保存了LoadedApk.ServiceDispatcher的binder引用,最后调用 c.conn.connected(r.name, service, false);

// LoadApk.InnerConnection
private static class InnerConnection extends IServiceConnection.Stub {
    final WeakReference<LoadedApk.ServiceDispatcher> mDispatcher;
    InnerConnection(LoadedApk.ServiceDispatcher sd) {
        mDispatcher = new WeakReference<LoadedApk.ServiceDispatcher>(sd);
    }
    public void connected(ComponentName name, IBinder service, boolean dead)
          throws RemoteException {
          LoadedApk.ServiceDispatcher sd = mDispatcher.get();
          if (sd != null) {
              sd.connected(name, service, dead);
          }
    }
}

这里可以看出InnerConnection就是一个binder,他的作用就是让其他进程可以跨进程调用到connected()。

最后在 LoadApk.InnerConnection利用ServiceDispatcher 调用了onServiceConnected()方法。

流程图

描述

总结

(没想好,待补上)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容