在android系统级服务启动中,状态栏右边的图标会被初始化,在左边一般显示动态的notification。这里我们要添加一个耳机插拔状态的图标,就应该放在右边,属于frameworks代码:
在android2.3中
frameworks/base/core/res/res/drawable-mdpi 加入要显示的图标 status_sys_headset.png
frameworks/base/core/res/res/values/config.xml 加入耳机字符串标记(在下面用到)
<string-array name="config_statusBarIcons">
<item><xliff:g id="id">ime</xliff:g></item>
<item><xliff:g id="id">sync_failing</xliff:g></item>
<item><xliff:g id="id">sync_active</xliff:g></item>
<item><xliff:g id="id">gps</xliff:g></item>
<item><xliff:g id="id">bluetooth</xliff:g></item>
<item><xliff:g id="id">nfc</xliff:g></item>
<item><xliff:g id="id">tty</xliff:g></item>
<item><xliff:g id="id">speakerphone</xliff:g></item>
<item><xliff:g id="id">mute</xliff:g></item>
<item><xliff:g id="id">volume</xliff:g></item>
<item ><xliff:g id="id"> headset</xliff:g></item >
<item><xliff:g id="id">wifi</xliff:g></item>
<item><xliff:g id="id">cdma_eri</xliff:g></item>
<item><xliff:g id="id">data_connection</xliff:g></item>
<item><xliff:g id="id">phone_evdo_signal</xliff:g></item>
<item><xliff:g id="id">phone_signal</xliff:g></item>
<item><xliff:g id="id">battery</xliff:g></item>
<item><xliff:g id="id">alarm_clock</xliff:g></item>
<item><xliff:g id="id">secure</xliff:g></item>
<item><xliff:g id="id">clock</xliff:g></item>
</string-array>
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/StatusBarPolicy.java 监听各个事件,处理状态栏图标变化。
模仿其他图标的添加:
private IBinder mHeadSetPlugIcon; private IconData mHeadSetPlugIconData;
在构造方法里: public StatusBarPolicy(Context context)
mService.setIcon("headset", R.drawable.status_sys_headset, 0);
mService.setIconVisibility("headset", false);
注册广播监听耳机:action tag可以在API文档中查找。
filter.addAction(Intent.ACTION_HEADSET_PLUG);
在广播接收onReceive(Context context, Intent intent) :
elseif (action.equals(Intent.ACTION_HEADSET_PLUG)) {
updateHeadset(intent);
}
定义方法改变耳机状态:
private void updateHeadset(Intent intent){
mService.setIconVisibility("headset", (intent.getIntExtra("state", 0) == 1 ) ? true : false)
}
下面时android2.3源码以前的版本添加方式。2.3以后这个已经放到应用中里:
1.在frameworks/base/core/res/res/drawable-mdpi 添加一个status_sys_headset.png。
2.修改frameworks/base/services/java/com/android/server/status/StatusBarPolicy.java
publicclass StatusBarPolicy { privatestaticfinal String TAG ="StatusBarPolicy" ; privatestatic StatusBarPolicy sInstance; ..... // alarm clock // Icon lit when clock is set private IBinder mAlarmClockIcon; private IconData mAlarmClockIconData; //HeadSet private IBinder mHeadSetPlugIcon; private IconData mHeadSetPlugIconData; ..... private BroadcastReceiver mIntentReceiver =new BroadcastReceiver() { @Override publicvoid onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_TIME_TICK)) { updateClock(); } ..... elseif (action.equals(Intent.ACTION_ALARM_CHANGED)) { updateAlarm(intent); } elseif (action.equals(Intent.ACTION_HEADSET_PLUG)) { updateHeadset(intent); } elseif (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(); //HeadSet mHeadSetPlugIconData = IconData.makeIcon( "headset" , null ,com.android.internal.R.drawable.status_sys_headset, 0 , 0 ); mHeadSetPlugIcon = service.addIcon(mHeadSetPlugIconData, null ); service.setIconVisibility(mHeadSetPlugIcon, false ); .... IntentFilter filter =new IntentFilter(); // Register for Intent broadcasts for... filter.addAction(Intent.ACTION_TIME_TICK); .... filter.addAction(Intent.ACTION_HEADSET_PLUG); .... } privatefinalvoid updateHeadset(Intent intent) { mService.setIconVisibility(mHeadSetPlugIcon, (intent.getIntExtra("state" , 0 ) ==1 )?true :false); } } 3.frameworks/base/core/res/res/values/arrays.xml 加入字段 <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 > <item ><xliff :g id ="id"> headset</xliff :g></item > <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" , null,
com.android.internal.R.drawable.status_sys_headset,0 ,0 );” 或者 mService.setIcon("headset", R.drawable.status_sys_headset, 0);中的第一个参数。“<item > <xliff :g id ="id"> headset</xliff :g> </item > ”这个语句的位置对应图标在状态栏上显示的位置顺序。