android 6.0 SystemUI源码分析(2)-SystemUI启动流程


1.SystemUI启动

SystemUI是核心系统应用,需要开机启动,启动SystemUI进程,是通过启动SystemUIService来实现的。


frameworks\base\services\java\com\android\server\SystemServer.java

SystemServer启动后,会在SystemServer Main Thread启动ActivityManagerService,当ActivityManagerService  systemReady后,会去启动SystemUIService。

 mActivityManagerService.systemReady(new Runnable() {
            @Override
            public void run() {
           ...
           try {
                    startSystemUi(context);
                } catch (Throwable e) {
                    reportWtf("starting System UI", e);
                }
由如上可以看出,startSystemUi不是在SystemServer Main thread,而是在ActivityManagerService Thread。

 static final void startSystemUi(Context context) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.android.systemui",
                    "com.android.systemui.SystemUIService"));
        //Slog.d(TAG, "Starting service: " + intent);
        context.startServiceAsUser(intent, UserHandle.OWNER);
    }
通过startServiceAsUser,SystemUIService就启动了,即SystemUI进程开机启动。


2.SystemUI Services启动

SystemServer启动SystemUIService后,会走到SystemUIService的onCreate函数。
public class SystemUIService extends Service {


    @Override
    public void onCreate() {
        super.onCreate();
        ((SystemUIApplication) getApplication()).startServicesIfNeeded();
    }
SystemUIService就是一个普通的Service,在onCreate里面,会调用SystemUIApplication的services

/**
 * Application class for SystemUI.
 */
public class SystemUIApplication extends Application {


    private static final String TAG = "SystemUIService";
    private static final boolean DEBUG = false;


    /**
     * The classes of the stuff to start.
     */
    private final Class<?>[] SERVICES = new Class[] {
            com.android.systemui.tuner.TunerService.class,
            com.android.systemui.keyguard.KeyguardViewMediator.class,
            com.android.systemui.recents.Recents.class,
            com.android.systemui.volume.VolumeUI.class,
            com.android.systemui.statusbar.SystemBars.class,
            com.android.systemui.usb.StorageNotification.class,
            com.android.systemui.power.PowerUI.class,
            com.android.systemui.media.RingtonePlayer.class,
    };

SystemUIApplication是一个Application实现,重写Application相关函数。
SystemUIApplication定义了很多System Panel,这里叫做SERVICES,但是并非是真正的service.

SystemUI应用定义了一个抽象的SystemUI类,根据Java抽象化的特征,可以使开发更加灵活。

SystemUI相关的类图关系如下:

从SystemUI继承了很多的Panel,这些Panel有我们很熟悉的,比如Recents(近期任务栏),VolumeUI(音量条),SystemBars(状态栏)等。


回到SystemUIApplication里的startService函数:
  1. /** 
  2.  * Makes sure that all the SystemUI services are running. If they are already running, this is a 
  3.  * no-op. This is needed to conditinally start all the services, as we only need to have it in 
  4.  * the main process. 
  5.  * 
  6.  * <p>This method must only be called from the main thread.</p> 
  7.  */  
  8. public void startServicesIfNeeded() {  
  9.     if (mServicesStarted) {  
  10.         return;  
  11.     }  
  12.   
  13.     if (!mBootCompleted) {  
  14.         // check to see if maybe it was already completed long before we began  
  15.         // see ActivityManagerService.finishBooting()  
  16.         if ("1".equals(SystemProperties.get("sys.boot_completed"))) {  
  17.             mBootCompleted = true;  
  18.             if (DEBUG) Log.v(TAG, "BOOT_COMPLETED was already sent");  
  19.         }  
  20.     }  
  21.   
  22.     Log.v(TAG, "Starting SystemUI services.");  
  23.     final int N = SERVICES.length;  
  24.     for (int i=0; i<N; i++) {  
  25.         Class<?> cl = SERVICES[i];  
  26.         if (DEBUG) Log.d(TAG, "loading: " + cl);  
  27.         try {  
  28.             mServices[i] = (SystemUI)cl.newInstance();  
  29.         } catch (IllegalAccessException ex) {  
  30.             throw new RuntimeException(ex);  
  31.         } catch (InstantiationException ex) {  
  32.             throw new RuntimeException(ex);  
  33.         }  
  34.         mServices[i].mContext = this;  
  35.         mServices[i].mComponents = mComponents;  
  36.         if (DEBUG) Log.d(TAG, "running: " + mServices[i]);  
  37.         mServices[i].start();  
  38.   
  39.         if (mBootCompleted) {  
  40.             mServices[i].onBootCompleted();  
  41.         }  
  42.     }  
  43.     mServicesStarted = true;  
  44. }  
这个函数主要是实例化以及启动SystemUI Services(这里的Service并非是真正的service),这样通过SystemUIService的启动,SystemUI核心的services也启动了。


在SystemUIApplication类的onCreate里面,会注册开机完成广播,并将开机完成事件,给到SystemUI Services.
  1. @Override  
  2.   public void onCreate() {  
  3.       super.onCreate();  
  4.       // Set the application theme that is inherited by all services. Note that setting the  
  5.       // application theme in the manifest does only work for activities. Keep this in sync with  
  6.       // the theme set there.  
  7.       setTheme(R.style.systemui_theme);  
  8.   
  9.       IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);  
  10.       filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);  
  11.       registerReceiver(new BroadcastReceiver() {  
  12.           @Override  
  13.           public void onReceive(Context context, Intent intent) {  
  14.               if (mBootCompleted) return;  
  15.   
  16.               if (DEBUG) Log.v(TAG, "BOOT_COMPLETED received");  
  17.               unregisterReceiver(this);  
  18.               mBootCompleted = true;  
  19.               if (mServicesStarted) {  
  20.                   final int N = mServices.length;  
  21.                   for (int i = 0; i < N; i++) {  
  22.                       mServices[i].onBootCompleted();  
  23.                   }  
  24.               }  
  25.           }  
  26.       }, filter);  
  27.   }  

SystemUI Services启动后,根据各Services的功能,SystemUI开始各司其职的正常工作起来。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值