1. 第一步声明Slot
其实就是个字符串而已。
frameworks/base/core/res/res/values/symbols.xml中加上声明
<java-symbol type="string" name="status_bar_input_type" />
2在frameworks/base/core/res/res/values/config.xml的string-array name="config_statusBarIcons"中添加我们自己的slot。比如:
<item><xliff:g id="id">@string/status_bar_input_type</xliff:g></item>
注意string-array name="config_statusBarIcons"中字符串添加的顺序就是图标在状态栏从左到右显示的顺序
然后在frameworks/base/core/res/res/values/config.xml继续添加
<string translatable="false" name="status_bar_input_type">input_type</string>
3在PhoneStatusBarPolicy.java中添加图标。
先声明一个slot 和一个广播aciton(action不是必要的)
private final String mSlotInputType;
private final String ACTION_INPUT_STATUS_CHANGED = "android.intent.action.INPUT_STATUS_CHANGED";
在构造方法中初始化
mSlotInputType = context.getString(com.android.internal.R.string.status_bar_input_type);
//...省略代码....
filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
//添加的action(非必要)
filter.addAction(ACTION_INPUT_STATUS_CHANGED);
mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
4. (非必要)
在mIntentReceiver广播接收器onReceive中调用按键状态改变后的方法,去改变图标显示。
case ACTION_INPUT_STATUS_CHANGED:
updateInputType(intent);
break;
5实现updateInputType方法
private final void updateInputType(Intent intent) {
mIconController.setIcon(mSlotInputType, R.drawable.y_button, null);
mIconController.setIconVisibility(mSlotInputType, true);
}
如果安装了SystemUI.apk之后图标顺序没改变或者没显示,可能是没有编译安装framework-res.apk,因为上面修改的config.xml文件不在SystemUI模块下,所有要编译framework-res.apk才能起效。该apk在/system/framework/framework-res.apk。编译framework-res.apk命令行:
make framework-res.apk
安装apk:
adb root
adb remount
adb push 编译好的apk路径名\framework-res.apk /system/framework/framework-res.apk
重新安装完framework-res.apk状态栏会显示正常
参考链接:Android状态栏右侧添加图标并控制其显示状态_android 在通知栏右侧显示图标_coder_soldier的博客-CSDN博客