本文代码基于Android 9.0分析,个人理解能力有限,如有不对请多指教。
SystemUI是在开机过程中SystemServer启动的,SystemServer的startOtherServices()中

/*frameworks/base/services/java/com/android/server/SystemServer.java*/private void startOtherServices() {...  try {      startSystemUi(context, windowManagerF);  } catch (Throwable e) {      reportWtf("starting System UI", e);  }...}//startSystemUi方法 static final void startSystemUi(Context context, WindowManagerService windowManager) {        Intent intent = new Intent();        intent.setComponent(new ComponentName("com.android.systemui",                    "com.android.systemui.SystemUIService"));        intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);        context.startServiceAsUser(intent, UserHandle.SYSTEM);        windowManager.onSystemUiStarted(); }

可以看到该方法会启动systemUi的SystemUIService服务。windowManager.onSystemUiStarted()方法最终会通过绑定的方式启动SystemUi的KeyguardService服务。

SystemUIService继承Service,是一个标准的android服务,所以必须重载onCreate接口:

//frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIService.java@Overridepublic void onCreate() {   super.onCreate();   ((SystemUIApplication) getApplication()).startServicesIfNeeded();   ... //省略不重要代码}

SystemUIService的onCreate方法中又调用了SystemUIApplication的startServicesIfNeeded方法。

//frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIApplication.javapublic void startServicesIfNeeded() {    String[] names = getResources().getStringArray(R.array.config_systemUIServiceComponents);    startServicesIfNeeded(names);}

先看下names里面存储的是什么。
config_systemUIServiceComponents:

<!-- frameworks/base/packages/SystemUI/res/values/config.xml --!><string-array name="config_systemUIServiceComponents" translatable="false">        <item>com.android.systemui.Dependency</item>        <item>com.android.systemui.util.NotificationChannels</item>        <item>com.android.systemui.statusbar.CommandQueue$CommandQueueStart</item>        <item>com.android.systemui.keyguard.KeyguardViewMediator</item>        <item>com.android.systemui.recents.Recents</item>        <item>com.android.systemui.volume.VolumeUI</item>        <item>com.android.systemui.stackdivider.Divider</item>        <item>com.android.systemui.SystemBars</item>        <item>com.android.systemui.usb.StorageNotification</item>        <item>com.android.systemui.power.PowerUI</item>        <item>com.android.systemui.media.RingtonePlayer</item>        <item>com.android.systemui.keyboard.KeyboardUI</item>        <item>com.android.systemui.pip.PipUI</item>        <item>com.android.systemui.shortcut.ShortcutKeyDispatcher</item>        <item>@string/config_systemUIVendorServiceComponent</item>        <item>com.android.systemui.util.leak.GarbageMonitor$Service</item>        <item>com.android.systemui.LatencyTester</item>        <item>com.android.systemui.globalactions.GlobalActionsComponent</item>        <item>com.android.systemui.ScreenDecorations</item>        <item>com.android.systemui.fingerprint.FingerprintDialogImpl</item>        <item>com.android.systemui.SliceBroadcastRelayHandler</item>    </string-array>

config_systemUIServiceComponents是一系列包名加类名。这些类都继承自SystemUI类,SystemUI是一个抽象类,其中有一个抽象方法

public abstract void start();

读取到String数组names里面,然后调用带参数的startServicesIfNeeded方法:

private void startServicesIfNeeded(String[] services) {     ...     //创建SystemUI数组,长度为names的长度     mServices = new SystemUI[services.length];...    //开始遍历services数组,也就是之前的names数组    final int N = services.length;    for (int i = 0; i < N; i++) {         String clsName = services[i];         ...         long ti = System.currentTimeMillis();         Class cls;         try {              cls = Class.forName(clsName);              //实例化取出的class              mServices[i] = (SystemUI) cls.newInstance();          } catch(ClassNotFoundException ex){             throw new RuntimeException(ex);          } catch (IllegalAccessException ex) {             throw new RuntimeException(ex);          } catch (InstantiationException ex) {             throw new RuntimeException(ex);          }  ...          mServices[i].mContext = this;          mServices[i].mComponents = mComponents;          if (DEBUG) Log.d(TAG, "running: " + mServices[i]);          //调用start方法启动          mServices[i].start();          ...          if (mBootCompleted) {              mServices[i].onBootCompleted();          }  ...   }

每一个系统ui元素(包括statusBar,PowerUI等)都必须继承SystemUI这个抽象类,并重载其中的start方法,这是一种比较灵活的编程方式,它允许我们在后期对系统UI元素进行轻松的扩展或者删除。具体每个UI元素的启动将在后续章节分析。

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. 箭头函数的基础使用
  3. Python list sort方法的具体使用
  4. python list.sort()根据多个关键字排序的方法实现
  5. android UI布局 layer-list
  6. Android应用程序启动时发生AndroidRuntime : ClassNotFoundExcep
  7. Android之获取屏幕和视图高和宽
  8. Android添加单元测试的方法与步骤
  9. android 开发的步骤

随机推荐

  1. android ndk 之Android.mk编写
  2. Android(安卓)Fragments新功能
  3. LocalBroadcastManager 的问题
  4. AndroidO中DRM的工作流程
  5. 使用AndroidStudio报错:INSTALL_FAILED_UP
  6. 内存分析与内存泄漏检测
  7. Recyclview实现仿京东淘宝金刚位带滚动进
  8. Android获取屏幕宽高
  9. android基础回顾---activity生命周期
  10. Android(安卓)使用scheme唤起app本地打开