修改状态栏(StatusBar)图标(icon)(定制自己的状态栏)

系统定制需要修改一下状态栏,如果想要刷机的话,网上资料挺多的,但是修改源代码的资料不多,在2.2中,源代码在frameworks/base/services/java/com/android/server/status/StatusBarPolicy.java和frameworks/base/services/java/com/android/server/status/StatusBarService.java中,在2.3中,源代码在:frameworks/base/package里面的SystemUI里面。

            在StatusBarPolicy中,添加图标的代码如下(404行):

  

[java] view plain copy
  1. public StatusBarPolicy(Context context) {  
  2.        mContext = context;  
  3.        mService = (StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE);  
  4.        mSignalStrength = new SignalStrength();  
  5.        mBatteryStats = BatteryStatsService.getService();  
  6.   
  7.        // storage  
  8.        mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);  
  9.        mStorageManager.registerListener(  
  10.                new com.android.systemui.usb.StorageNotification(context));  
  11.   
  12.        // battery  
  13.        mService.setIcon("battery", com.android.internal.R.drawable.stat_sys_battery_unknown, 0);  
  14.   
  15.        // phone_signal  
  16.        mPhone = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);  
  17.        mPhoneSignalIconId = R.drawable.stat_sys_signal_null;  
  18.        mService.setIcon("phone_signal", mPhoneSignalIconId, 0);  
  19.        mService.setIconVisibility("phone_signal",false);  
  20.   
  21.        // register for phone state notifications.  
  22.        ((TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE))  
  23.                .listen(mPhoneStateListener,  
  24.                          PhoneStateListener.LISTEN_SERVICE_STATE  
  25.                        | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS  
  26.                        | PhoneStateListener.LISTEN_CALL_STATE  
  27.                        | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE  
  28.                        | PhoneStateListener.LISTEN_DATA_ACTIVITY);  
  29.   
  30.        // data_connection  
  31.        mService.setIcon("data_connection", R.drawable.stat_sys_data_connected_g, 0);  
  32.        mService.setIconVisibility("data_connection"false);  
  33.   
  34.        // wifi  
  35.        mService.setIcon("wifi", sWifiSignalImages[0][0], 0);  
  36.        mService.setIconVisibility("wifi"false);  
  37.        // wifi will get updated by the sticky intents  
  38.   
  39.        // TTY status  
  40.        mService.setIcon("tty",  R.drawable.stat_sys_tty_mode, 0);  
  41.        mService.setIconVisibility("tty"false);  
  42.   
  43.        // Cdma Roaming Indicator, ERI  
  44.        mService.setIcon("cdma_eri", R.drawable.stat_sys_roaming_cdma_0, 0);  
  45.        mService.setIconVisibility("cdma_eri"false);  
  46.   
  47.        // bluetooth status  
  48.        mService.setIcon("bluetooth", R.drawable.stat_sys_data_bluetooth, 0);  
  49.        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();  
  50.        if (adapter != null) {  
  51.            mBluetoothEnabled = adapter.isEnabled();  
  52.        } else {  
  53.            mBluetoothEnabled = false;  
  54.        }  
  55.        mBluetoothA2dpConnected = false;  
  56.        mBluetoothHeadsetState = BluetoothHeadset.STATE_DISCONNECTED;  
  57.        mBluetoothPbapState = BluetoothPbap.STATE_DISCONNECTED;  
  58.        mService.setIconVisibility("bluetooth", mBluetoothEnabled);  
  59.   
  60.        // Gps status  
  61.        mService.setIcon("gps", R.drawable.stat_sys_gps_acquiring_anim, 0);  
  62.        mService.setIconVisibility("gps"false);  
  63.   
  64.        // Alarm clock  
  65.        mService.setIcon("alarm_clock", R.drawable.stat_notify_alarm, 0);  
  66.        mService.setIconVisibility("alarm_clock"false);  
  67.   
  68.        // Sync state  
  69.        mService.setIcon("sync_active", com.android.internal.R.drawable.stat_notify_sync_anim0, 0);  
  70.        mService.setIcon("sync_failing", com.android.internal.R.drawable.stat_notify_sync_error, 0);  
  71.        mService.setIconVisibility("sync_active"false);  
  72.        mService.setIconVisibility("sync_failing"false);  
  73.   
  74.        // volume  
  75.        mService.setIcon("volume", R.drawable.stat_sys_ringer_silent, 0);  
  76.        mService.setIconVisibility("volume"false);  
  77.        updateVolume();  
  78.   
  79.        IntentFilter filter = new IntentFilter();  
  80.   
  81.        // Register for Intent broadcasts for...  
  82.        filter.addAction(Intent.ACTION_BATTERY_CHANGED);  
  83.        filter.addAction(Intent.ACTION_BATTERY_LOW);  
  84.        filter.addAction(Intent.ACTION_BATTERY_OKAY);  
  85.        filter.addAction(Intent.ACTION_POWER_CONNECTED);  
  86.        filter.addAction(Intent.ACTION_ALARM_CHANGED);  
  87.        filter.addAction(Intent.ACTION_SYNC_STATE_CHANGED);  
  88.        filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);  
  89.        filter.addAction(AudioManager.VIBRATE_SETTING_CHANGED_ACTION);  
  90.        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);  
  91.        filter.addAction(BluetoothHeadset.ACTION_STATE_CHANGED);  
  92.        filter.addAction(BluetoothA2dp.ACTION_SINK_STATE_CHANGED);  
  93.        filter.addAction(BluetoothPbap.PBAP_STATE_CHANGED_ACTION);  
  94.        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);  
  95.        filter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);  
  96.        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);  
  97.        filter.addAction(WifiManager.RSSI_CHANGED_ACTION);  
  98.        filter.addAction(LocationManager.GPS_ENABLED_CHANGE_ACTION);  
  99.        filter.addAction(LocationManager.GPS_FIX_CHANGE_ACTION);  
  100.        filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);  
  101.        filter.addAction(TtyIntent.TTY_ENABLED_CHANGE_ACTION);  
  102.        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);  
  103.        filter.addAction(ConnectivityManager.INET_CONDITION_ACTION);  
  104.        mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);  
  105.   
  106.        // load config to determine if to distinguish Hspa data icon  
  107.        try {  
  108.            mHspaDataDistinguishable = mContext.getResources().getBoolean(  
  109.                    R.bool.config_hspa_data_distinguishable);  
  110.        } catch (Exception e) {  
  111.            mHspaDataDistinguishable = false;  
  112.        }  
  113.    }  

下面简单说一下用法,获取StatusBarManager:

private final StatusBarManager mService;

[java] view plain copy
  1. mService = (StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE);        
  2.   
  3.   
  4.   
  5. <span style="font-size:14px;">下面是获得图标:获取以后处理</span>  
  6. // phone_signal  
  7.         mPhone = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);  
  8.         mPhoneSignalIconId = R.drawable.stat_sys_signal_null;  
  9.         mService.setIcon("phone_signal", mPhoneSignalIconId, 0);  
  10.         mService.setIconVisibility("phone_signal",false);  


在这里,我把这个信号图标给隐藏了,所以当你编译编译源码,生成自己SDK,然后新建一个模拟器,就会发现信号图标已经没有了,如果你想改变布局,或者更改图标,在res里面找一下相应的文件,今天就写到这,其余信息,继续补充。

你可能感兴趣的:(修改状态栏(StatusBar)图标(icon)(定制自己的状态栏))