system ui 1

来源:互联网 发布:java之父 aws 编辑:程序博客网 时间:2024/05/21 03:20
systemUI是在systemserver中起来的
xref: /frameworks/base/services/java/com/android/server/SystemServer.java
1112                try {
1113                    startSystemUi(context);
1114                } catch (Throwable e) {
1115                    reportWtf("starting System UI", e);
1116                }
我们来看看startSystemUi 是如何启动system UI的
1223    static final void startSystemUi(Context context) {
1224        Intent intent = new Intent();
1225        intent.setComponent(new ComponentName("com.android.systemui",
1226                    "com.android.systemui.SystemUIService"));
1227        //Slog.d(TAG, "Starting service: " + intent);
1228        context.startServiceAsUser(intent, UserHandle.OWNER);
1229    }


可以看到是通过Intent来设置包名启动SystemUIService 这个server
xref: /frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIService.java
public class SystemUIService extends Service {
27
28    @Override
29    public void onCreate() {
30        super.onCreate();
31        ((SystemUIApplication) getApplication()).startServicesIfNeeded();
32    }
}


SystemUIService  是Service的子类,在其onCreate函数函数中调用startServicesIfNeeded
xref: /frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
   public void startServicesIfNeeded() {
98        if (mServicesStarted) {
99            return;
100        }
101
102        if (!mBootCompleted) {
103            // check to see if maybe it was already completed long before we began
104            // see ActivityManagerService.finishBooting()
105            if ("1".equals(SystemProperties.get("sys.boot_completed"))) {
106                mBootCompleted = true;
107                if (DEBUG) Log.v(TAG, "BOOT_COMPLETED was already sent");
108            }
109        }
110
111        Log.v(TAG, "Starting SystemUI services.");
112        final int N = SERVICES.length;
113        for (int i=0; i<N; i++) {
114            Class<?> cl = SERVICES[i];
115            if (DEBUG) Log.d(TAG, "loading: " + cl);
116            try {
117                mServices[i] = (SystemUI)cl.newInstance();
118            } catch (IllegalAccessException ex) {
119                throw new RuntimeException(ex);
120            } catch (InstantiationException ex) {
121                throw new RuntimeException(ex);
122            }
123            mServices[i].mContext = this;
124            mServices[i].mComponents = mComponents;
125            if (DEBUG) Log.d(TAG, "running: " + mServices[i]);
126            mServices[i].start();
127
128            if (mBootCompleted) {
129                mServices[i].onBootCompleted();
130            }
131        }
132        mServicesStarted = true;
133    }
通过反射启动SERVICES中的类
42    private final Class<?>[] SERVICES = new Class[] {
43            com.android.systemui.tuner.TunerService.class,
44            com.android.systemui.keyguard.KeyguardViewMediator.class,
45            com.android.systemui.recents.Recents.class,
46            com.android.systemui.volume.VolumeUI.class,
47            com.android.systemui.statusbar.SystemBars.class,
48            com.android.systemui.usb.StorageNotification.class,
49            com.android.systemui.power.PowerUI.class,
50            com.android.systemui.media.RingtonePlayer.class,
51            com.android.systemui.keyboard.KeyboardUI.class,
52    };


所以system Ui需要启动的class如上所示.
需要注意的是SystemUIApplication 的onCreate函数中 会注册一个高优先级的Intent,来监听BOOT_COMPLETED 广播。
如果监听到了,就取消这个BroadcastReceive,并调用这些service的onBootCompleted方法.
xref: /frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
 public void onCreate() {
64        super.onCreate();
65        // Set the application theme that is inherited by all services. Note that setting the
66        // application theme in the manifest does only work for activities. Keep this in sync with
67        // the theme set there.
68        setTheme(R.style.systemui_theme);
69
70        IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
71        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
72        registerReceiver(new BroadcastReceiver() {
73            @Override
74            public void onReceive(Context context, Intent intent) {
75                if (mBootCompleted) return;
76
77                if (DEBUG) Log.v(TAG, "BOOT_COMPLETED received");
78                unregisterReceiver(this);
79                mBootCompleted = true;
80                if (mServicesStarted) {
81                    final int N = mServices.length;
82                    for (int i = 0; i < N; i++) {
83                        mServices[i].onBootCompleted();
84                    }
85                }
86            }
87        }, filter);
88    }
0 0
原创粉丝点击