Android4.0-4.4 加入支持状态栏显示耳机图标方法(支持带不带MIC的两种耳机自己主动识别)

时间:2023-11-21 15:40:50

效果如图:

Android4.0-4.4 加入支持状态栏显示耳机图标方法(支持带不带MIC的两种耳机自己主动识别)

一、 在frameworks/base/packages/SystemUI/res/values/strings.xml 里加入

  1. <string name="headset_enabled">Headset Enabled.</string>

二、 在fameworks/base/core/res/res/values/config.xml 里对应位置加入:

  1. @@ -45,6 +45,7 @@
  2. <item><xliff:g id="id">alarm_clock</xliff:g></item>
  3. <item><xliff:g id="id">secure</xliff:g></item>
  4. <item><xliff:g id="id">clock</xliff:g></item>
  5. +       <item><xliff:g id="id">headset</xliff:g></item>
  6. </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 打上以下的补丁:

  1. --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
  2. +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
  3. @@ -103,6 +103,9 @@ public class PhoneStatusBarPolicy {
  4. else if (action.equals(TtyIntent.TTY_ENABLED_CHANGE_ACTION)) {
  5. updateTTY(intent);
  6. }
  7. +            else if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
  8. +                updateHeadset(intent);
  9. +            }
  10. }
  11. };
  12. @@ -119,6 +122,7 @@ public class PhoneStatusBarPolicy {
  13. filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
  14. filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
  15. filter.addAction(TtyIntent.TTY_ENABLED_CHANGE_ACTION);
  16. +        filter.addAction(Intent.ACTION_HEADSET_PLUG);
  17. mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
  18. int numPhones = MSimTelephonyManager.getDefault().getPhoneCount();
  19. @@ -276,4 +280,29 @@ public class PhoneStatusBarPolicy {
  20. mService.setIconVisibility("tty", false);
  21. }
  22. }
  23. +
  24. +    private final void updateHeadset(Intent intent) {
  25. +        final String action = intent.getAction();
  26. +        final int state = intent.getIntExtra("state", 4);
  27. +        final int mic = intent.getIntExtra("microphone", 4);
  28. +
  29. +        switch (state) {
  30. +        case 0:
  31. +            try{
  32. +                mService.setIconVisibility("headset", false);
  33. +            } catch (Exception e) {
  34. +                //Log.i("StatusBar Headset", "frist time to run");
  35. +                }
  36. +        break;
  37. +        case 1:
  38. +            if (mic == 1)
  39. +                mService.setIcon("headset", R.drawable.stat_sys_headset_mic, 0,
  40. +                    mContext.getResources().getString(R.string.headset_enabled));
  41. +            else
  42. +                mService.setIcon("headset", R.drawable.stat_sys_headset, 0,
  43. +                    mContext.getResources().getString(R.string.headset_enabled));
  44. +            mService.setIconVisibility("headset", true);
  45. +        break;
  46. +        }
  47. +    }
  48. }

smali版本号參考本人github上的lewa的patchrom代码:

https://github.com/syhost/lewa_patchrom_ef65l/commit/202a790d5c3e4dfb8fdfb6e837d96fd69e79d448

当然要结合上面的源代码, 只作为參考 由于跟你的smali代码应该会差异非常大