Home>

As shown in the figure, when the headset is plugged in,A headset icon appears on the status bar.

The icon of the headset and the signal of the sim card are on the right of the status bar, so this implementation is definitely not implemented through notification, so adding this function can only be found in framwork.The specific modification steps are as follows:

1. First add a stat_sys_headset.png resource file in the frameworks/base/core/res/res/drawable-mdpi directory.The directory of the resource files should be determined according to the resolution of the device.My device is hvga, so the resource files are placed in the drawable-mdpi directory.

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

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.Modify frameworks/base/core/res/res/values ​​/arrays.xml

<string-array name="status_bar_icon_order">
  <item><xliff:g>clock</xliff:g></item>
  <item><xliff:g>secure</xliff:g></item>
  <item><xliff:g>alarm_clock</xliff:g></item>
  <item><xliff:g>battery</xliff:g></item>
  <item><xliff:g>phone_signal_2</xliff:g></item>
  <item><xliff:g>phone_signal</xliff:g></item>
  <item><xliff:g>phone_evdo_signal</xliff:g></item>
  <item><xliff:g>data_connection</xliff:g></item>
  <item><xliff:g>cdma_eri</xliff:g></item>
  <item><xliff:g>tty</xliff:g></item>
  <item><xliff:g>volume</xliff:g></item>
  <item><xliff:g>mute</xliff:g></item>
  <item><xliff:g>speakerphone</xliff:g></item>
  <!-Modify here start.->
  <item><xliff:g>headset</xliff:g></item>
  <!-Modify here end->
  <item><xliff:g>wifi</xliff:g></item>
  <item><xliff:g>tty</xliff:g></item>
  <item><xliff:g>bluetooth</xliff:g></item>
  <item><xliff:g>gps</xliff:g></item>
  <item><xliff:g>sync_active</xliff:g></item>
  <item><xliff:g>sync_failing</xliff:g></item>
  <item><xliff:g>ime</xliff:g></item>
</string-array>

The "headset" string is the first parameter in "mheadsetplugicondata=icondata.makeicon (" headset ", null0, 0);"."<item><xliff:g>headset</xliff:g></item>" The position of this statement is related to where the icon is displayed on the status bar.

  • Previous Android implementation of the function of adding status bar icon
  • Next Android implementation of customized desktop method