一、在 SystemUI 中添加抽象函数(frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUI.java)
public abstract class SystemUI {
public Context mContext;
public abstract void start();
public abstract void onReceive(String action);//添加抽象函数,注意只要继承了SystemUI的实例也都需要重写onReceive方法,否则会编译出错
protected void onConfigurationChanged(Configuration newConfig) {
}
public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
}
}
注意在如下文件中需要重写onReceive
Android 4.0
frameworks/base/packages/SystemUI/src/com/android/systemui/power/PowerUI.java
Anroid 4.2
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
frameworks/base/packages/SystemUI/src/com/android/systemui/media/RingtonePlayer.java
frameworks/base/packages/SystemUI/src/com/android/systemui/power/PowerUI.java
即在类里面多添加一条如下代码即可
public void onReceive(String action){}
二、在 SystemUIService 中注册 BroadcastReceiver (frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIService.java)
先定义action string
public static final String ACTION_DISPLAY_STATUS_BAR = "android.intent.action.DISPLAY_STATUS_BAR";
public static final String ACTION_HIDE_STATUS_BAR = "android.intent.action.HIDE_STATUS_BAR";
注册广播代码
@Override
public void onCreate() {
// Pick status bar or system bar.
IWindowManager wm = IWindowManager.Stub.asInterface(
ServiceManager.getService(Context.WINDOW_SERVICE));
try {
Log.d(TAG, "*** canStatusBarHide = " + wm.canStatusBarHide() + " ***");
SERVICES[0] = wm.canStatusBarHide()
? R.string.config_statusBarComponent
: R.string.config_systemBarComponent;
} catch (RemoteException e) {
Slog.w(TAG, "Failing checking whether status bar can hide", e);
}
final int N = SERVICES.length;
mServices = new SystemUI[N];
for (int i=0; i<N; i++) {
Class cl = chooseClass(SERVICES&