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

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

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

android实现添加耳机状态图标的方法_第1张图片

如图,当插入耳机后,在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

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


  clock
  secure
  alarm_clock
  battery
  phone_signal_2
  phone_signal
  phone_evdo_signal
  data_connection
  cdma_eri
  tty
  volume
  mute
  speakerphone
  
  headset
  
  wifi
  tty
  bluetooth
  gps
  sync_active
  sync_failing
  ime

其中“headset”这个字符串就是“mHeadSetPlugIconData = IconData.makeIcon( "headset", null0, 0);”中的第一个参数。“headset ”这个语句的位置于图标在status bar上显示的位置有关。

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

你可能感兴趣的:(android实现添加耳机状态图标的方法)