日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 5.1 SystemServer SystemService 各个系统Manager

發布時間:2025/3/15 Android 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 5.1 SystemServer SystemService 各个系统Manager 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、SystemServer

Zygote如何啟動SystemServer就不分析了,主要分析下java層:

先看下主函數

[java]?view plaincopy
  • public?static?void?main(String[]?args)?{??
  • ????new?SystemServer().run();??
  • }??
  • 下面我們看run函數里面的java service的啟動:

    [java]?view plaincopy
  • mSystemServiceManager?=?new?SystemServiceManager(mSystemContext);??
  • LocalServices.addService(SystemServiceManager.class,?mSystemServiceManager);??
  • ??
  • //?Start?services.??
  • 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加入自己的鏈表。并且調用service的onStart函數。

    [java]?view plaincopy
  • public?<T?extends?SystemService>?T?startService(Class<T>?serviceClass)?{??
  • ????final?String?name?=?serviceClass.getName();??
  • ????Slog.i(TAG,?"Starting?"?+?name);??
  • ??
  • ????//?Create?the?service.??
  • ????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);??
  • ????}??
  • ??
  • ????//?Register?it.??
  • ????mServices.add(service);//加入鏈表??
  • ??
  • ????//?Start?it.??
  • ????try?{??
  • ????????service.onStart();//調用service的onStart函數??
  • ????}?catch?(RuntimeException?ex)?{??
  • ????????throw?new?RuntimeException("Failed?to?start?service?"?+?name??
  • ????????????????+?":?onStart?threw?an?exception",?ex);??
  • ????}??
  • ????return?service;??
  • }??
  • SystemServer每執行到一個階段都會調用SystemServiceManager的startBootPhase函數

    [java]?view plaincopy
  • mActivityManagerService?=?mSystemServiceManager.startService(??
  • ????????ActivityManagerService.Lifecycle.class).getService();??
  • mActivityManagerService.setSystemServiceManager(mSystemServiceManager);??
  • mActivityManagerService.setInstaller(installer);??
  • ??
  • //?Power?manager?needs?to?be?started?early?because?other?services?need?it.??
  • //?Native?daemons?may?be?watching?for?it?to?be?registered?so?it?must?be?ready??
  • //?to?handle?incoming?binder?calls?immediately?(including?being?able?to?verify??
  • //?the?permissions?for?those?calls).??
  • mPowerManagerService?=?mSystemServiceManager.startService(PowerManagerService.class);??
  • ??
  • //?Now?that?the?power?manager?has?been?started,?let?the?activity?manager??
  • //?initialize?power?management?features.??
  • mActivityManagerService.initPowerManagement();??
  • ??
  • //?Display?manager?is?needed?to?provide?display?metrics?before?package?manager??
  • //?starts?up.??
  • mDisplayManagerService?=?mSystemServiceManager.startService(DisplayManagerService.class);??
  • ??
  • //?We?need?the?default?display?before?we?can?initialize?the?package?manager.??
  • mSystemServiceManager.startBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY);??
  • 我們下面看下SystemServiceManager的startBootPhase函數,它會把mServices中的service取出來,調用onBootPhase函數

    [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這個類,這個類一般是一些系統service來繼承這個類

    先看看PowerManagerService

    [java]?view plaincopy
  • public?final?class?PowerManagerService?extends?SystemService??
  • ????????implements?Watchdog.Monitor?{??
  • 其中實現了onStart和onBootPhase函數

    [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函數是SystemService中的函數,最后是調用了ServiceManager.addService。

    [java]?view plaincopy
  • protected?final?void?publishBinderService(String?name,?IBinder?service)?{??
  • ????publishBinderService(name,?service,?false);??
  • }??
  • ??
  • /**?
  • ?*?Publish?the?service?so?it?is?accessible?to?other?services?and?apps.?
  • ?*/??
  • 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;??
  • ????????}??
  • ??
  • ????????//?Find?the?service?manager??
  • ????????sServiceManager?=?ServiceManagerNative.asInterface(BinderInternal.getContextObject());??
  • ????????return?sServiceManager;??
  • ????}??
  • ??
  • ????/**?
  • ?????*?Returns?a?reference?to?a?service?with?the?given?name.?
  • ?????*??
  • ?????*?@param?name?the?name?of?the?service?to?get?
  • ?????*?@return?a?reference?to?the?service,?or?<code>null</code>?if?the?service?doesn't?exist?
  • ?????*/??
  • ????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;??
  • ????}??
  • ??
  • ????/**?
  • ?????*?Place?a?new?@a?service?called?@a?name?into?the?service?
  • ?????*?manager.?
  • ?????*??
  • ?????*?@param?name?the?name?of?the?new?service?
  • ?????*?@param?service?the?service?object?
  • ?????*/??
  • ????public?static?void?addService(String?name,?IBinder?service)?{??
  • ????????try?{??
  • ????????????getIServiceManager().addService(name,?service,?false);??
  • ????????}?catch?(RemoteException?e)?{??
  • ????????????Log.e(TAG,?"error?in?addService",?e);??
  • ????????}??
  • ????}??

  • 四、系統的各個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
  • /*package*/?static?class?ServiceFetcher?{??
  • ????int?mContextCacheIndex?=?-1;??
  • ??
  • ????/**?
  • ?????*?Main?entrypoint;?only?override?if?you?don't?need?caching.?
  • ?????*/??
  • ????public?Object?getService(ContextImpl?ctx)?{??
  • ????????ArrayList<Object>?cache?=?ctx.mServiceCache;??
  • ????????Object?service;??
  • ????????synchronized?(cache)?{??
  • ????????????if?(cache.size()?==?0)?{??
  • ????????????????//?Initialize?the?cache?vector?on?first?access.??
  • ????????????????//?At?this?point?sNextPerContextServiceCacheIndex??
  • ????????????????//?is?the?number?of?potential?services?that?are??
  • ????????????????//?cached?per-Context.??
  • ????????????????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;??
  • ????????}??
  • ????}??
  • ??
  • ????/**?
  • ?????*?Override?this?to?create?a?new?per-Context?instance?of?the?
  • ?????*?service.??getService()?will?handle?locking?and?caching.?
  • ?????*/??
  • ????public?Object?createService(ContextImpl?ctx)?{??
  • ????????throw?new?RuntimeException("Not?implemented");??
  • ????}??
  • } ?
  • 原文地址: http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/kc58236582/article/details/48784563

    總結

    以上是生活随笔為你收集整理的Android 5.1 SystemServer SystemService 各个系统Manager的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。