Android4.0-4.4 加入支持状态栏显示耳机图标方法(支持带不带MIC的两种耳机自己主动识别)
效果如图:
一、 在frameworks/base/packages/SystemUI/res/values/strings.xml 里加入
- <string name="headset_enabled">Headset Enabled.</string>
二、 在fameworks/base/core/res/res/values/config.xml 里对应位置加入:
- @@ -45,6 +45,7 @@
- <item><xliff:g id="id">alarm_clock</xliff:g></item>
- <item><xliff:g id="id">secure</xliff:g></item>
- <item><xliff:g id="id">clock</xliff:g></item>
- + <item><xliff:g id="id">headset</xliff:g></item>
- </string-array>
上面带+号的行为加入的
三、 frameworks/base/packages/SystemUI/res/drawable-xhdpi 或者你手机相应的分辨率的目录下加入stat_sys_headset.png和stat_sys_headset_mic.png
两个图片, 分别表示不带mic的耳机和带mic的耳机, 这两个图标将在状态栏显示, 图片能够自己找,也能够从fameworks/base/core/res/res/drawable-xhdpi里面提取现成的
四、 在frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java 打上以下的补丁:
- --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
- +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
- @@ -103,6 +103,9 @@ public class PhoneStatusBarPolicy {
- else if (action.equals(TtyIntent.TTY_ENABLED_CHANGE_ACTION)) {
- updateTTY(intent);
- }
- + else if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
- + updateHeadset(intent);
- + }
- }
- };
- @@ -119,6 +122,7 @@ public class PhoneStatusBarPolicy {
- filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
- filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
- filter.addAction(TtyIntent.TTY_ENABLED_CHANGE_ACTION);
- + filter.addAction(Intent.ACTION_HEADSET_PLUG);
- mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
- int numPhones = MSimTelephonyManager.getDefault().getPhoneCount();
- @@ -276,4 +280,29 @@ public class PhoneStatusBarPolicy {
- mService.setIconVisibility("tty", false);
- }
- }
- +
- + private final void updateHeadset(Intent intent) {
- + final String action = intent.getAction();
- + final int state = intent.getIntExtra("state", 4);
- + final int mic = intent.getIntExtra("microphone", 4);
- +
- + switch (state) {
- + case 0:
- + try{
- + mService.setIconVisibility("headset", false);
- + } catch (Exception e) {
- + //Log.i("StatusBar Headset", "frist time to run");
- + }
- + break;
- + case 1:
- + if (mic == 1)
- + mService.setIcon("headset", R.drawable.stat_sys_headset_mic, 0,
- + mContext.getResources().getString(R.string.headset_enabled));
- + else
- + mService.setIcon("headset", R.drawable.stat_sys_headset, 0,
- + mContext.getResources().getString(R.string.headset_enabled));
- + mService.setIconVisibility("headset", true);
- + break;
- + }
- + }
- }
smali版本号參考本人github上的lewa的patchrom代码:
https://github.com/syhost/lewa_patchrom_ef65l/commit/202a790d5c3e4dfb8fdfb6e837d96fd69e79d448
当然要结合上面的源代码, 只作为參考 由于跟你的smali代码应该会差异非常大