电池状态

引用:http://blog.csdn.net/ouyangtianhan/article/details/7197987

想使用Android系统的电池侦听和系统的电池图片;

image

 

 

引入源代码中的两个xml文件(这里我在Android2.2的jar包中找不到,所以才引入,源代码路径:C:\android-sdk-windows\platforms\android-8\data\res\drawable),之后会发现stat_sys_battery.xml报错,所以我就将下图

 

 

image

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);  
posted @ 2013-01-31 10:01  镇水古月  阅读(460)  评论(0编辑  收藏  举报