android实现添加耳机状态图标的方法

时间:2022-09-23 17:56:06

本文实例讲述了android实现添加耳机状态图标的方法。分享给大家供大家参考。具体如下:

原生态的android系统是没有耳机插入或未插入的状态指示的,本文就是讲解如何添加耳机插入的状态指示。效果图如下

android实现添加耳机状态图标的方法

如图,当插入耳机后,在status bar上出现了一个耳机的图标。

这个耳机的图标和sim卡信号等图标在status bar的右边,因此这个实现肯定不是通过notification实现的,那么添加这个功能只能在framwork里面寻找了。具体的修改步骤如下:

1.在frameworks/base/core/res/res/drawable-mdpi目录先添加一个stat_sys_headset.png资源文件。这里面资源文件的目录要根据设备的分辨率来确定,我的设备是hvga的,因此资源文件放在drawable-mdpi目录下。

2.修改frameworks/base/services/java/com/android/server/status/statusbarpolicy.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class statusbarpolicy {
 private static final string tag = "statusbarpolicy";
 private static statusbarpolicy sinstance;
 .....
 // alarm clock
 // icon lit when clock is set
 private ibinder malarmclockicon;
 private icondata malarmclockicondata;
 //modify here start
 //headset
 private ibinder mheadsetplugicon;
 private icondata mheadsetplugicondata;
 //modify here end
 .....
 private broadcastreceiver mintentreceiver = new broadcastreceiver() {
  @override
  public void onreceive(context context, intent intent) {
   string action = intent.getaction();
   if (action.equals(intent.action_time_tick)) {
    updateclock();
   }
   .....
   else if (action.equals(intent.action_alarm_changed)) {
    updatealarm(intent);
   }
   //modify here start
   else if (action.equals(intent.action_headset_plug)) {
    updateheadset(intent);
   }
   //modify here end
   else if (action.equals(intent.action_sync_state_changed)) {
    updatesyncstate(intent);
   }
   ......
  };
  private statusbarpolicy(context context, statusbarservice service) {
  mcontext = context;
  mservice = service;
  msignalstrength = new signalstrength();
  if(featureoption.mtk_gemini_support == true) {
   msignalstrengthgemini = new signalstrength();
  }
  mbatterystats = batterystatsservice.getservice();
  ....
  //modify here start
  //headset
  mheadsetplugicondata = icondata.makeicon(
   "headset",
   null,com.android.internal.r.drawable.stat_sys_headset, 0, 0);
  mheadsetplugicon = service.addicon(mheadsetplugicondata, null);
  service.seticonvisibility(mheadsetplugicon, false);
  //modify here end
  ....
  intentfilter filter = new intentfilter();
  // register for intent broadcasts for...
  filter.addaction(intent.action_time_tick);
  ....
  //modify here start
  filter.addaction(intent.action_headset_plug);
  //modify here end
  ....
 }
 //modify here start
 private final void updateheadset(intent intent) {
  slog.d(tag, "updateheadset: state=" + intent.getintextra("state", 0));
  mservice.seticonvisibility(mheadsetplugicon, (intent.getintextra("state", 0) == 1)?true:false);
 }
 //modify here end
 .....
}

3.修改frameworks/base/core/res/res/values/arrays.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<string-array name="status_bar_icon_order">
  <item><xliff:g id="id">clock</xliff:g></item>
  <item><xliff:g id="id">secure</xliff:g></item>
  <item><xliff:g id="id">alarm_clock</xliff:g></item>
  <item><xliff:g id="id">battery</xliff:g></item>
  <item><xliff:g id="id">phone_signal_2</xliff:g></item>
  <item><xliff:g id="id">phone_signal</xliff:g></item>
  <item><xliff:g id="id">phone_evdo_signal</xliff:g></item>
  <item><xliff:g id="id">data_connection</xliff:g></item>
  <item><xliff:g id="id">cdma_eri</xliff:g></item>
  <item><xliff:g id="id">tty</xliff:g></item>
  <item><xliff:g id="id">volume</xliff:g></item>
  <item><xliff:g id="id">mute</xliff:g></item>
  <item><xliff:g id="id">speakerphone</xliff:g></item>
  <!-- modify here start. -->
  <item><xliff:g id="id">headset</xliff:g></item>
  <!-- modify here end -->
  <item><xliff:g id="id">wifi</xliff:g></item>
  <item><xliff:g id="id">tty</xliff:g></item>
  <item><xliff:g id="id">bluetooth</xliff:g></item>
  <item><xliff:g id="id">gps</xliff:g></item>
  <item><xliff:g id="id">sync_active</xliff:g></item>
  <item><xliff:g id="id">sync_failing</xliff:g></item>
  <item><xliff:g id="id">ime</xliff:g></item>
</string-array>

其中“headset”这个字符串就是“mheadsetplugicondata = icondata.makeicon( "headset", null0, 0);”中的第一个参数。“<item><xliff:g id="id">headset</xliff:g></item> ”这个语句的位置于图标在status bar上显示的位置有关。

希望本文所述对大家的android程序设计有所帮助。