安卓系统框架学习_startService启动流程(安卓9.0)(未完成)

安卓系统框架学习_startService启动流程(安卓9.0)

1:进程A(发起调用端)

方法调用过程:
MyActivity.startService() -> ContentWrapper#startService -> ContentImpl#startService -> ContentImpl#startServiceCommon
看startServiceCommon()方法源码:

private ComponentName startServiceCommon(Intent service, boolean requireForeground,
UserHandle user) {
			... ...
            ComponentName cn = ActivityManager.getService().startService(
                mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
                            getContentResolver()), requireForeground,
                            getOpPackageName(), user.getIdentifier());  
            return cn;
    }

实际上ActivityManager.getService()内部只有一行代码:

return IActivityManagerSingleton.get();

其中, IActivityManagerSingletonAvtivityManager类中的一个私有静态成员, 在类加载时加载到内存, 其内部进行了一次IActivityManager的创建. 然后通过get()方法获取IActivityManager实例.

private static final Singleton<IActivityManager> IActivityManagerSingleton =
           new Singleton<IActivityManager>() {
               @Override
               protected IActivityManager create() {
                   final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
                   final IActivityManager am = IActivityManager.Stub.asInterface(b);
                   return am; } };

所以在startServiceCommon()方法中的ActivityManager.getService().startService()实际上就是IActivityManager.startService().
也就是说,这整个过程可以简化为``"进程A调用startService方法,本质上是通过binder通信,调用IActivityManager#startService方法.

2:system_server端

SystemServer进程的binder线程响应此请求,ActivityManagerService#startService方法被调用.

方法调用过程:
ActivityManagerService#startActivity -> ActivityService#startServiceLocked ->ActivityService#startServiceInnerLocked -> ActivityService#bringUpServiceLocked

你可能感兴趣的:(安卓系统框架学习)