Android7.0在状态栏添加图标,定制系统framework

先说一下我的功能需求。定制andorid系统,有个mic控制键,当点击这个键时,会在状态栏出现mic状态的图标。

1.frameworks/base / core/res/res/values/config.xml

 
       ... ...
       @string/status_bar_mic_mute
 
      ... ...
  mic_mute
2.core/ res/res/values/symbols.xml
3.  frameworks/base / packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java

 //MIC
        mIconController.setIcon(mSlotMicMute,com.android.internal.R.drawable.ic_mic_mute,null);
        mIconController.setIconVisibility(mSlotMicMute, false);

        // TTY status
        mIconController.setIcon(mSlotTty,  R.drawable.stat_sys_tty_mode, null);
        mIconController.setIconVisibility(mSlotTty, false);

        // bluetooth status
        updateBluetooth();

        // Alarm clock
        mIconController.setIcon(mSlotAlarmClock, R.drawable.stat_sys_alarm, null);
        mIconController.setIconVisibility(mSlotAlarmClock, false);
如其他代码一样,添加mic的代码。其中的 mSlotMicMute变量,请按照mSlotTty的方式去得到。

mSlotMicMute = context.getString(com.android.internal.R.string.status_bar_mic_mute);
需要在framework/base/core/res/res/values/strings.xml中定义。(向安卓系统中添加图片,文字等资源的方法,请在我的博客中查找《 向安卓系统中添加图片,文字等资源》)

4.frameworks/base / packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java

IntentFilter filter = new IntentFilter();
        filter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
        filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
        filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION);
        filter.addAction(AudioManager.ACTION_HEADSET_PLUG);
        filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
        filter.addAction(TelecomManager.ACTION_CURRENT_TTY_MODE_CHANGED);
        filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
        filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
        filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
        //mic mute
        filter.addAction("android.intent.action.MIC_MUTE_BROADCAST");
        filter.addAction("android.intent.action.MIC_NO_MUTE_BROADCAST");
        mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
动态添加action 为android.Intent.action.MIC_MUTE_BORADCAST 的广播。

5.在你需要控制的地方发送广播

6.接收并处理广播

private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED)) {
                updateAlarm();
            } 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_MANAGED_PROFILE_AVAILABLE) ||
                    action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE) ||
                    action.equals(Intent.ACTION_MANAGED_PROFILE_REMOVED)) {
                updateQuietState();
                updateManagedProfile();
            } else if (action.equals(AudioManager.ACTION_HEADSET_PLUG)) {
                updateHeadsetPlug(intent);
            }else if (action.equals("android.intent.action.MIC_MUTE_BROADCAST")){
     //接收广播并处理图片的显示和消失
      mIconController.setIconVisibility(mSlotMicMute, true);
            }else if (action.equals("android.intent.action.MIC_NO_MUTE_BROADCAST")){
                mIconController.setIconVisibility(mSlotMicMute, false);
            }
        }
    };



你可能感兴趣的:(android系统)