该系列文章总纲链接:专题分纲目录 Android Framework 音频子系统
本章关键点总结 & 说明:
本章节主要关注➕ 以上思维导图左上 耳麦插拔 部分中的应用案例部分 即可。这里主要以一个案例进行说明,在有些Android版本中并不会在状态栏上显示耳麦图标,需要我们自己添加。我们这个应用端的需求就是插拔耳机,会在状态栏中设置相应的图标状态。
接下来看具体怎么做?在PhoneStatusBarPolicy中注册这个Intent,代码如下:
public PhoneStatusBarPolicy(Context context, CastController cast, HotspotController hotspot) {mContext = context;mCast = cast;mHotspot = hotspot;mService = (StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE);// listen for broadcastsIntentFilter filter = new IntentFilter();filter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);filter.addAction(Intent.ACTION_SYNC_STATE_CHANGED);filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION);filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);filter.addAction(TelecomManager.ACTION_CURRENT_TTY_MODE_CHANGED);filter.addAction(Intent.ACTION_USER_SWITCHED);//新添加filter.addAction(Intent.ACTION_HEADSET_PLUG);//...
}
当收到消息后onReceive会被调用(updateHeadset是处理函数),代码如下:
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (action.equals(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED)) {updateAlarm();}else if (action.equals(Intent.ACTION_SYNC_STATE_CHANGED)) {updateSyncState(intent);}else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED) ||action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {updateBluetooth();}else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION) ||action.equals(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION)) {updateVolumeZen();}else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {updateSimState(intent);}else if (action.equals(TelecomManager.ACTION_CURRENT_TTY_MODE_CHANGED)) {updateTTY(intent);}else if (action.equals(Intent.ACTION_USER_SWITCHED)) {updateAlarm();}//新添加,收到消息后onReceive会被调用,updateHeadset是对应的处理函数else if (action.equals(Intent.ACTION_USER_SWITCHED)) {updateAlarm();}}};
updateHeadset这个处理函数是我们新添加的,主要功能是在状态栏中设置head set图标的状态,代码如下:
private final void updateHeadset(Intent intent) {final String action = intent.getAction();final int state = intent.getIntExtra("state", 4);final int mic = intent.getIntExtra("microphone", 4);switch (state) {case 0: //拔出mService.setIconVisibility("headset", false);break;case 1: //插入if (mic == 1) { //耳机上有mic显示这张图标mService.setIcon("headset", R.drawable.stat_sys_headset_with_mic, 0, null);} else {//耳机上没有mic显示这张图标mService.setIcon("headset", R.drawable.stat_sys_headset_without_mic, 0, null);}mService.setIconVisibility("headset", true);break;}
}
这里注意一些资源文件的配置和添加, 修改 frameworks/base/core/res/res/values/config.xml 添加一行:
<item><xliff:g id="id">headset</xliff:g></item>
根据自己设备的实际分辨率 创建对应的图标文件:
- frameworks/base/packages/SystemUI/res/"分辨率"/stat_sys_headset_with_mic.png
- frameworks/base/packages/SystemUI/res/"分辨率"/stat_sys_headset_without_mic.png
这样以来,一个简单的功能(耳麦插拔->状态栏图标变化)就实现了。