在android系统SystemUI里面添加耳机图标的一点知识

原生态的android系统中在状态栏貌似没有耳机这个图标显示,下面我们可以通过这个方法添加一个耳机的图标:

系统状态栏要显示的图标,都会在Framework/base/core/res/res/values的两个xml文件里添加图标要显示的位置信息。
config.xml:

 
    com.android.systemui/com.android.systemui.statusbar.StatusBarService

   
   
       ime
       sync_failing
       sync_active
       gps
       bluetooth
       nfc
       tty
       speakerphone
      
       headset
      
       mute
       volume
       wifi
       cdma_eri
       no_sim_card2
       phone_signal_second_sub
       data_connection
       phone_evdo_signal
       no_sim_card1
       phone_signal
       battery
       alarm_clock
       secure
       clock
   

arrays.xml


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

接下来在drawable里添加耳机图片,耳机大小不能超过25*25,否则会出现显示被压缩的。

系统所有的图标控制显示都是在SystemUI的StatusBarPolicy.java文件里
在这里我们就可以添加服务和广播,监听耳机插拔的事件如下:

private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
                updateBattery(intent);
            }
            else if (action.equals(Intent.ACTION_ALARM_CHANGED)) {
                updateAlarm(intent);
              //耳机广播
            }else if(action.equals(Intent.ACTION_HEADSET_PLUG)){
             updateHeadset(intent); 
              //耳机广播
            }else if (action.equals(Intent.ACTION_SYNC_STATE_CHANGED)) {
                updateSyncState(intent);
            }
            else if (action.equals(Intent.ACTION_BATTERY_LOW)) {
                onBatteryLow(intent);
            }
            else if (action.equals(Intent.ACTION_BATTERY_OKAY)
                    || action.equals(Intent.ACTION_POWER_CONNECTED)) {
                onBatteryOkay(intent);
            }
            else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED) ||
                    action.equals(BluetoothHeadset.ACTION_STATE_CHANGED) ||
                    action.equals(BluetoothA2dp.ACTION_SINK_STATE_CHANGED) ||
                    action.equals(BluetoothPbap.PBAP_STATE_CHANGED_ACTION)) {
                updateBluetooth(intent);
            }
            else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION) ||
                    action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION) ||
                    action.equals(WifiManager.RSSI_CHANGED_ACTION)) {
                updateWifi(intent);
            }
            else if (action.equals(LocationManager.GPS_ENABLED_CHANGE_ACTION) ||
                    action.equals(LocationManager.GPS_FIX_CHANGE_ACTION)) {
                updateGps(intent);
            }
            else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION) ||
                    action.equals(AudioManager.VIBRATE_SETTING_CHANGED_ACTION)) {
                updateVolume();
            }
            else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
                updateSimState(intent);
            }
            else if (action.equals(TtyIntent.TTY_ENABLED_CHANGE_ACTION)) {
                updateTTY(intent);
            }
            else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION) ||
                     action.equals(ConnectivityManager.INET_CONDITION_ACTION)) {
                // TODO - stop using other means to get wifi/mobile info
                updateConnectivity(intent);
            }
            else if (action.equals(WimaxManagerConstants.WIMAX_ENABLED_STATUS_CHANGED) ||
                     action.equals(WimaxManagerConstants.SIGNAL_LEVEL_CHANGED_ACTION) ||
                     action.equals(WimaxManagerConstants.WIMAX_STATE_CHANGED_ACTION)) {
                updateWiMAX(intent);
            }
        }
    };

然后在初始化StatusBarPolicy里加入服务 public StatusBarPolicy(Context context) 

 //headset
        mService.setIcon("headset", R.drawable.stat_sys_headset, 0);
        mService.setIconVisibility("headset", false);

自定义一个类:

  //耳机图标显示控制
    private final void updateHeadset(Intent intent) {
        final String action = intent.getAction();
  if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
   int state = intent.getIntExtra("state", 0);
   if (state == 1) {
    mService.setIconVisibility("headset", true);
   } else {
    mService.setIconVisibility("headset", false);
   }
  }
    }

行了,这样我们就可以在状态栏看到刚添加的耳机图标。OK!呵呵。。。当然系统底层也需要这样一个广播发送哦!

 

 

 

你可能感兴趣的:(在android系统SystemUI里面添加耳机图标的一点知识)