歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android 電池狀態偵聽顯示 電量 充電動態顯示

想使用Android系統的電池偵聽和系統的電池圖片;

引入源代碼中的兩個xml文件(這裡我在android2.2的jar包中找不到,所以才引入,源代碼路徑:C:\android-sdk-windows\platforms\android-8\data\res\drawable),之後會發現stat_sys_battery.xml報錯,所以我就將下圖

 

1.這些圖引入到我的drawable目錄下,然後將stat_sys_battery.xml中的圖片引入路徑更改,如下:

  1. <level-list xmlns:android="http://schemas.android.com/apk/res/android">   
  2.     <item android:maxLevel="4" android:drawable="@drawable/stat_sys_battery_0" />   
  3.     <item android:maxLevel="14" android:drawable="@drawable/stat_sys_battery_10" />   
  4.     <item android:maxLevel="29" android:drawable="@drawable/stat_sys_battery_20" />   
  5.     <item android:maxLevel="49" android:drawable="@drawable/stat_sys_battery_40" />   
  6.     <item android:maxLevel="69" android:drawable="@drawable/stat_sys_battery_60" />   
  7.     <item android:maxLevel="89" android:drawable="@drawable/stat_sys_battery_80" />   
  8.     <item android:maxLevel="100" android:drawable="@drawable/stat_sys_battery_100" />   
  9. </level-list>  
2.接下來是在activity中的操作:

   創建一個IntentFilter

  1. <span style="white-space:pre">    </span>private IntentFilter mIntentFilter;   
    在oncreate的方法中:加入:
  1. <span style="white-space:pre">    </span>//電池相關    
  2. <span style="white-space:pre">    </span>mIntentFilter = new IntentFilter();    
  3. <span style="white-space:pre">    </span>mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);   
3.接收廣播
  1. <span style="white-space:pre">    </span>//聲明消息處理過程     
  2.        private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {    
  3.            @Override    
  4.            public void onReceive(Context context, Intent intent) {    
  5.                String action = intent.getAction();    
  6.                int status = intent.getIntExtra("status"0);   
  7.                int health = intent.getIntExtra("health"0);   
  8.                boolean present = intent.getBooleanExtra("present"false);   
  9.                int level = intent.getIntExtra("level"0);   
  10.                int scale = intent.getIntExtra("scale"0);   
  11.                int icon_small = intent.getIntExtra("icon-small"0);   
  12.                int plugged = intent.getIntExtra("plugged"0);   
  13.                int voltage = intent.getIntExtra("voltage"0);   
  14.                int temperature = intent.getIntExtra("temperature"0);   
  15.                String technology = intent.getStringExtra("technology");   
  16.                String statusString = "";   
  17.                switch (status) {   
  18.                case BatteryManager.BATTERY_STATUS_UNKNOWN:   
  19.                    statusString = "unknown";   
  20.                    break;   
  21.                case BatteryManager.BATTERY_STATUS_CHARGING:   
  22.                    statusString = "charging";   
  23.                    battery_image.setImageResource(R.drawable.stat_sys_battery_charge);   
  24.                    battery_image.getDrawable().setLevel(level);   
  25.                    break;   
  26.                case BatteryManager.BATTERY_STATUS_DISCHARGING:   
  27.                    statusString = "discharging";   
  28.                    battery_image.setImageResource(R.drawable.stat_sys_battery);   
  29.                    battery_image.getDrawable().setLevel(level);   
  30.                    break;   
  31.                case BatteryManager.BATTERY_STATUS_NOT_CHARGING:   
  32.                    statusString = "not charging";   
  33.                    break;   
  34.                case BatteryManager.BATTERY_STATUS_FULL:   
  35.                    statusString = "full";   
  36.                    break;   
  37.                }   
  38.                String healthString = "";   
  39.                switch (health) {   
  40.                case BatteryManager.BATTERY_HEALTH_UNKNOWN:   
  41.                    healthString = "unknown";   
  42.                    break;   
  43.                case BatteryManager.BATTERY_HEALTH_GOOD:   
  44.                    healthString = "good";   
  45.                    break;   
  46.                case BatteryManager.BATTERY_HEALTH_OVERHEAT:   
  47.                    healthString = "overheat";   
  48.                    break;   
  49.                case BatteryManager.BATTERY_HEALTH_DEAD:   
  50.                    healthString = "dead";   
  51.                    break;   
  52.                case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:   
  53.                    healthString = "voltage";   
  54.                    break;   
  55.                case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:   
  56.                    healthString = "unspecified failure";   
  57.                    break;   
  58.                }   
  59.                String acString = "";   
  60.                switch (plugged) {   
  61.                case BatteryManager.BATTERY_PLUGGED_AC:   
  62.                    acString = "plugged ac";   
  63.                    break;   
  64.                case BatteryManager.BATTERY_PLUGGED_USB:   
  65.                    acString = "plugged usb";   
  66.                    break;   
  67.                }   
  68.                Log.i("cat", statusString);   
  69.                Log.i("cat", healthString);   
  70.                Log.i("cat", String.valueOf(present));   
  71.                Log.i("cat", String.valueOf(level));   
  72.                Log.i("cat", String.valueOf(scale));   
  73.                Log.i("cat", String.valueOf(icon_small));   
  74.                Log.i("cat", acString);   
  75.                Log.i("cat", String.valueOf(voltage));   
  76.                Log.i("cat", String.valueOf(temperature));   
  77.                Log.i("cat", technology);   
  78.                //要看看是不是我們要處理的消息     
  79.                if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {               
  80.                    //電池電量,數字     
  81.                    Log.d("Battery""" + intent.getIntExtra("level"0));                   
  82.                    //電池最大容量     
  83.                    Log.d("Battery""" + intent.getIntExtra("scale"0));                   
  84.                    //電池伏數     
  85.                    Log.d("Battery""" + intent.getIntExtra("voltage"0));                   
  86.                    //電池溫度     
  87.                    Log.d("Battery""" + intent.getIntExtra("temperature"0));    
  88.                    //電池狀態,返回是一個數字     
  89.                    // BatteryManager.BATTERY_STATUS_CHARGING 表示是充電狀態     
  90.                    // BatteryManager.BATTERY_STATUS_DISCHARGING 放電中     
  91.                    // BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充電     
  92.                    // BatteryManager.BATTERY_STATUS_FULL 電池滿     
  93.                    Log.d("Battery""ss" + intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_CHARGING));    
  94.                    //充電類型 BatteryManager.BATTERY_PLUGGED_AC 表示是充電器,不是這個值,表示是 USB     
  95.                    Log.d("Battery""" + intent.getIntExtra("plugged"0));    
  96.                    //電池健康情況,返回也是一個數字     
  97.                    //BatteryManager.BATTERY_HEALTH_GOOD 良好     
  98.                    //BatteryManager.BATTERY_HEALTH_OVERHEAT 過熱     
  99.                    //BatteryManager.BATTERY_HEALTH_DEAD 沒電     
  100.                    //BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE 過電壓     
  101.                    //BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE 未知錯誤     
  102.                    Log.d("Battery""" + intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN));    
  103.                }    
  104.            }    
  105.        };   
    這裡真正處理圖片變換的是以下幾句話:其他的我是在做備注
  1. <span style="font-weight: bold; white-space: pre; ">  </span>switch (status) {   
  2.                case BatteryManager.BATTERY_STATUS_UNKNOWN:   
  3.                    statusString = "unknown";   
  4.                    break;   
  5.                case BatteryManager.BATTERY_STATUS_CHARGING:   
  6.                    statusString = "charging";   
  7.                    battery_image.setImageResource(R.drawable.stat_sys_battery_charge);   
  8.                    battery_image.getDrawable().setLevel(level);   
  9.                    break;   
  10.                case BatteryManager.BATTERY_STATUS_DISCHARGING:   
  11.                    statusString = "discharging";   
  12.                    battery_image.setImageResource(R.drawable.stat_sys_battery);  
  13.                    battery_image.getDrawable().setLevel(level);   
  14.                    break;   
  15.                case BatteryManager.BATTERY_STATUS_NOT_CHARGING:   
  16.                    statusString = "not charging";   
  17.                    break;   
  18.                case BatteryManager.BATTERY_STATUS_FULL:   
  19.                    statusString = "full";   
  20.                    break;   
  21.                }  
4.在onResume方法中注冊消息處理器 
  1. <span style="white-space:pre">    </span>registerReceiver(mIntentReceiver, mIntentFilter);  
5.在onPause方法中注銷
  1. <span style="white-space:pre">    </span>unregisterReceiver(mIntentReceiver);  
Copyright © Linux教程網 All Rights Reserved