基于Android4.4 systemUIService.java 类分析
首先创建了并引用了一个类的数组mServices。数组类的成员均继承systemUI
private final Class<?>[] SERVICES = new Class[] {
com.android.systemui.recent.Recents.class, // 最近应用
com.android.systemui.statusbar.SystemBars.class, // 系统状态栏
com.android.systemui.usb.StorageNotification.class, // storage存储通知
com.android.systemui.power.PowerUI.class, // 电量管理相关
com.android.systemui.media.RingtonePlayer.class, //铃声播放
com.android.systemui.settings.SettingsUI.class, // 设置相关
};
/**
* Hold a reference on the stuff we start.
*/
private final SystemUI[] mServices = new SystemUI[SERVICES.length];
在onCreate() 中遍历数组中成员初始化并执行start()
public void onCreate() {
HashMap<Class<?>, Object> components = new HashMap<Class<?>, Object>();
final int N = SERVICES.length;
for (int i=0; i<N; i++) {
Class<?> cl = SERVICES[i];
Log.d(TAG, "loading: " + cl);
try {
mServices[i] = (SystemUI)cl.newInstance();
} catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
} catch (InstantiationException ex) {
throw new RuntimeException(ex);
}
mServices[i].mContext = this; //
mServices[i].mComponents = components;
Log.d(TAG, "running: " + mServices[i]);
mServices[i].start();
}
}
同时在该类中重写onConfigurationChanged()方法并调用mServices数组中所有成员的onConfigurationChanged()方法,进而实现对 “相关事件改变” 执行对应的动作。
public void onConfigurationChanged(Configuration newConfig) {
for (SystemUI ui: mServices) {
ui.onConfigurationChanged(newConfig);
}
}