一、SystemServer
Zygote如何啟動SystemServer就不分析了,主要分析下java層:
先看下主函數(shù)
[java]?view plaincopy
public?static?void?main(String[]?args)?{?? ????new?SystemServer().run();?? }??
下面我們看run函數(shù)里面的java service的啟動:
[java]?view plaincopy
mSystemServiceManager?=?new?SystemServiceManager(mSystemContext);?? LocalServices.addService(SystemServiceManager.class,?mSystemServiceManager);?? ?? ?? try?{?? ????startBootstrapServices();?? ????startCoreServices();?? ????startOtherServices();?? }?catch?(Throwable?ex)?{?? ????Slog.e("System",?"******************************************");?? ????Slog.e("System",?"************?Failure?starting?system?services",?ex);?? ????throw?ex;?? }??
SystemServiceManager這個類只是管理各個service,用SystemServiceManager啟動service的時候,會把service加入自己的鏈表。并且調(diào)用service的onStart函數(shù)。
[java]?view plaincopy
public?<T?extends?SystemService>?T?startService(Class<T>?serviceClass)?{?? ????final?String?name?=?serviceClass.getName();?? ????Slog.i(TAG,?"Starting?"?+?name);?? ?? ?????? ????if?(!SystemService.class.isAssignableFrom(serviceClass))?{?? ????????throw?new?RuntimeException("Failed?to?create?"?+?name?? ????????????????+?":?service?must?extend?"?+?SystemService.class.getName());?? ????}?? ????final?T?service;?? ????try?{?? ????????Constructor<T>?constructor?=?serviceClass.getConstructor(Context.class);?? ????????service?=?constructor.newInstance(mContext);?? ????}?catch?(InstantiationException?ex)?{?? ????????throw?new?RuntimeException("Failed?to?create?service?"?+?name?? ????????????????+?":?service?could?not?be?instantiated",?ex);?? ????}?catch?(IllegalAccessException?ex)?{?? ????????throw?new?RuntimeException("Failed?to?create?service?"?+?name?? ????????????????+?":?service?must?have?a?public?constructor?with?a?Context?argument",?ex);?? ????}?catch?(NoSuchMethodException?ex)?{?? ????????throw?new?RuntimeException("Failed?to?create?service?"?+?name?? ????????????????+?":?service?must?have?a?public?constructor?with?a?Context?argument",?ex);?? ????}?catch?(InvocationTargetException?ex)?{?? ????????throw?new?RuntimeException("Failed?to?create?service?"?+?name?? ????????????????+?":?service?constructor?threw?an?exception",?ex);?? ????}?? ?? ?????? ????mServices.add(service);?? ?? ?????? ????try?{?? ????????service.onStart();?? ????}?catch?(RuntimeException?ex)?{?? ????????throw?new?RuntimeException("Failed?to?start?service?"?+?name?? ????????????????+?":?onStart?threw?an?exception",?ex);?? ????}?? ????return?service;?? }??
SystemServer每執(zhí)行到一個階段都會調(diào)用SystemServiceManager的startBootPhase函數(shù)
[java]?view plaincopy
mActivityManagerService?=?mSystemServiceManager.startService(?? ????????ActivityManagerService.Lifecycle.class).getService();?? mActivityManagerService.setSystemServiceManager(mSystemServiceManager);?? mActivityManagerService.setInstaller(installer);?? ?? ?? ?? ?? ?? mPowerManagerService?=?mSystemServiceManager.startService(PowerManagerService.class);?? ?? ?? ?? mActivityManagerService.initPowerManagement();?? ?? ?? ?? mDisplayManagerService?=?mSystemServiceManager.startService(DisplayManagerService.class);?? ?? ?? mSystemServiceManager.startBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY);??
我們下面看下SystemServiceManager的startBootPhase函數(shù),它會把mServices中的service取出來,調(diào)用onBootPhase函數(shù)
[java]?view plaincopy
public?void?startBootPhase(final?int?phase)?{?? ????if?(phase?<=?mCurrentPhase)?{?? ????????throw?new?IllegalArgumentException("Next?phase?must?be?larger?than?previous");?? ????}?? ????mCurrentPhase?=?phase;?? ?? ????Slog.i(TAG,?"Starting?phase?"?+?mCurrentPhase);?? ?? ????final?int?serviceLen?=?mServices.size();?? ????for?(int?i?=?0;?i?<?serviceLen;?i++)?{?? ????????final?SystemService?service?=?mServices.get(i);?? ????????try?{?? ????????????service.onBootPhase(mCurrentPhase);?? ????????}?catch?(Exception?ex)?{?? ????????????throw?new?RuntimeException("Failed?to?boot?service?"?? ????????????????????+?service.getClass().getName()?? ????????????????????+?":?onBootPhase?threw?an?exception?during?phase?"?? ????????????????????+?mCurrentPhase,?ex);?? ????????}?? ????}?? }??
二、SystemService
我們再來SystemService這個類,這個類一般是一些系統(tǒng)service來繼承這個類
先看看PowerManagerService
[java]?view plaincopy
public?final?class?PowerManagerService?extends?SystemService?? ????????implements?Watchdog.Monitor?{??
其中實現(xiàn)了onStart和onBootPhase函數(shù)
[java]?view plaincopy
@Override?? public?void?onStart()?{?? ????publishBinderService(Context.POWER_SERVICE,?new?BinderService());?? ????publishLocalService(PowerManagerInternal.class,?new?LocalService());?? ?? ????Watchdog.getInstance().addMonitor(this);?? ????Watchdog.getInstance().addThread(mHandler);?? }?? ?? @Override?? public?void?onBootPhase(int?phase)?{?? ????synchronized?(mLock)?{?? ????????if?(phase?==?PHASE_BOOT_COMPLETED)?{?? ????????????final?long?now?=?SystemClock.uptimeMillis();?? ????????????mBootCompleted?=?true;?? ????????????mDirty?|=?DIRTY_BOOT_COMPLETED;?? ????????????userActivityNoUpdateLocked(?? ????????????????????now,?PowerManager.USER_ACTIVITY_EVENT_OTHER,?0,?Process.SYSTEM_UID);?? ????????????updatePowerStateLocked();?? ????????}?? ????}?? }??
其中publishBinderService函數(shù)是SystemService中的函數(shù),最后是調(diào)用了ServiceManager.addService。
[java]?view plaincopy
protected?final?void?publishBinderService(String?name,?IBinder?service)?{?? ????publishBinderService(name,?service,?false);?? }?? ?? ? ? ?? protected?final?void?publishBinderService(String?name,?IBinder?service,?? ????????boolean?allowIsolated)?{?? ????ServiceManager.addService(name,?service,?allowIsolated);?? }??
三、ServiceManager
而ServiceManager 這個類,是java層用來和serviceManager進程通信的。
[java]?view plaincopy
public?final?class?ServiceManager?{?? ????private?static?final?String?TAG?=?"ServiceManager";?? ?? ????private?static?IServiceManager?sServiceManager;?? ????private?static?HashMap<String,?IBinder>?sCache?=?new?HashMap<String,?IBinder>();?? ?? ????private?static?IServiceManager?getIServiceManager()?{?? ????????if?(sServiceManager?!=?null)?{?? ????????????return?sServiceManager;?? ????????}?? ?? ?????????? ????????sServiceManager?=?ServiceManagerNative.asInterface(BinderInternal.getContextObject());?? ????????return?sServiceManager;?? ????}?? ?? ????? ? ? ? ? ?? ????public?static?IBinder?getService(String?name)?{?? ????????try?{?? ????????????IBinder?service?=?sCache.get(name);?? ????????????if?(service?!=?null)?{?? ????????????????return?service;?? ????????????}?else?{?? ????????????????return?getIServiceManager().getService(name);?? ????????????}?? ????????}?catch?(RemoteException?e)?{?? ????????????Log.e(TAG,?"error?in?getService",?e);?? ????????}?? ????????return?null;?? ????}?? ?? ????? ? ? ? ? ? ?? ????public?static?void?addService(String?name,?IBinder?service)?{?? ????????try?{?? ????????????getIServiceManager().addService(name,?service,?false);?? ????????}?catch?(RemoteException?e)?{?? ????????????Log.e(TAG,?"error?in?addService",?e);?? ????????}?? ????}??
四、系統(tǒng)的各個Manager
再來看看ContextImpl里面對各個Manager的注冊,下面是PMS的注冊
[java]?view plaincopy
registerService(POWER_SERVICE,?new?ServiceFetcher()?{?? ????????public?Object?createService(ContextImpl?ctx)?{?? ????????????IBinder?b?=?ServiceManager.getService(POWER_SERVICE);?? ????????????IPowerManager?service?=?IPowerManager.Stub.asInterface(b);?? ????????????if?(service?==?null)?{?? ????????????????Log.wtf(TAG,?"Failed?to?get?power?manager?service.");?? ????????????}?? ????????????return?new?PowerManager(ctx.getOuterContext(),?? ????????????????????service,?ctx.mMainThread.getHandler());?? ????????}});??
再來看看getSystemService,比如獲取PowerManager等
[java]?view plaincopy
@Override?? public?Object?getSystemService(String?name)?{?? ????ServiceFetcher?fetcher?=?SYSTEM_SERVICE_MAP.get(name);?? ????return?fetcher?==?null???null?:?fetcher.getService(this);?? }??
下面ServiceFetcher 中createService就是上面在注冊各個Manager的時候定義的。
[java]?view plaincopy
?static?class?ServiceFetcher?{?? ????int?mContextCacheIndex?=?-1;?? ?? ????? ? ?? ????public?Object?getService(ContextImpl?ctx)?{?? ????????ArrayList<Object>?cache?=?ctx.mServiceCache;?? ????????Object?service;?? ????????synchronized?(cache)?{?? ????????????if?(cache.size()?==?0)?{?? ?????????????????? ?????????????????? ?????????????????? ?????????????????? ????????????????for?(int?i?=?0;?i?<?sNextPerContextServiceCacheIndex;?i++)?{?? ????????????????????cache.add(null);?? ????????????????}?? ????????????}?else?{?? ????????????????service?=?cache.get(mContextCacheIndex);?? ????????????????if?(service?!=?null)?{?? ????????????????????return?service;?? ????????????????}?? ????????????}?? ????????????service?=?createService(ctx);?? ????????????cache.set(mContextCacheIndex,?service);?? ????????????return?service;?? ????????}?? ????}?? ?? ????? ? ? ?? ????public?Object?createService(ContextImpl?ctx)?{?? ????????throw?new?RuntimeException("Not?implemented");?? ????}?? } ?
原文地址: http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/kc58236582/article/details/48784563
總結(jié)
以上是生活随笔為你收集整理的Android 5.1 SystemServer SystemService 各个系统Manager的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。