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
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
<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程序设计有所帮助。
赞 (0)