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
 private ibinder mheadsetplugicon;
 private icondata mheadsetplugicondata;
 //modify here end
 private broadcastreceiver mintentreceiver=new broadcastreceiver () {
  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) {
  msignalstrength=new signalstrength ();
  if (featureoption.mtk_gemini_support == true) {
   msignalstrengthgemini=new signalstrength ();
  mbatterystats=batterystatsservice.getservice ();
  //modify here start
  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">
  <!-Modify here start.->
  <!-Modify here end->

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