欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现侦听电池状态显示、电量及充电动态显示的方法

程序员文章站 2022-08-17 19:44:18
本文实例讲述了android实现侦听电池状态显示、电量及充电动态显示的方法,是android应用程序开发中非常常用的重要功能。分享给大家供大家参考之用。具体方法如下: 使...

本文实例讲述了android实现侦听电池状态显示、电量及充电动态显示的方法,是android应用程序开发中非常常用的重要功能。分享给大家供大家参考之用。具体方法如下:

使用android系统的电池侦听和系统的电池图片如下;

Android实现侦听电池状态显示、电量及充电动态显示的方法

引入源代码中的两个xml文件(这里我在android2.2的jar包中找不到,所以才引入,源代码路径:c:\android-sdk-windows\platforms\android-8\data\res\drawable),之后会发现stat_sys_battery.xml报错,如下图所示:
 
Android实现侦听电池状态显示、电量及充电动态显示的方法

1.这些图引入到我的drawable目录下,然后将stat_sys_battery.xml中的图片引入路径更改,如下:

<level-list xmlns:android="http://schemas.android.com/apk/res/android"> 
 <item android:maxlevel="4" android:drawable="@drawable/stat_sys_battery_0" /> 
 <item android:maxlevel="14" android:drawable="@drawable/stat_sys_battery_10" /> 
 <item android:maxlevel="29" android:drawable="@drawable/stat_sys_battery_20" /> 
 <item android:maxlevel="49" android:drawable="@drawable/stat_sys_battery_40" /> 
 <item android:maxlevel="69" android:drawable="@drawable/stat_sys_battery_60" /> 
 <item android:maxlevel="89" android:drawable="@drawable/stat_sys_battery_80" /> 
 <item android:maxlevel="100" android:drawable="@drawable/stat_sys_battery_100" /> 
</level-list>

2.接下来是在activity中的操作:

创建一个intentfilter

private intentfilter mintentfilter; 

在oncreate的方法中:加入:

//电池相关 
mintentfilter = new intentfilter(); 
mintentfilter.addaction(intent.action_battery_changed); 

3.接收广播

//声明消息处理过程 
private broadcastreceiver mintentreceiver = new broadcastreceiver() { 
  @override 
  public void onreceive(context context, intent intent) { 
   string action = intent.getaction(); 
   int status = intent.getintextra("status", 0); 
   int health = intent.getintextra("health", 0); 
   boolean present = intent.getbooleanextra("present", false); 
   int level = intent.getintextra("level", 0); 
   int scale = intent.getintextra("scale", 0); 
   int icon_small = intent.getintextra("icon-small", 0); 
   int plugged = intent.getintextra("plugged", 0); 
   int voltage = intent.getintextra("voltage", 0); 
   int temperature = intent.getintextra("temperature", 0); 
   string technology = intent.getstringextra("technology"); 
   string statusstring = ""; 
   switch (status) { 
   case batterymanager.battery_status_unknown: 
    statusstring = "unknown"; 
    break; 
   case batterymanager.battery_status_charging: 
    statusstring = "charging"; 
    battery_image.setimageresource(r.drawable.stat_sys_battery_charge); 
    battery_image.getdrawable().setlevel(level); 
    break; 
   case batterymanager.battery_status_discharging: 
    statusstring = "discharging"; 
    battery_image.setimageresource(r.drawable.stat_sys_battery); 
    battery_image.getdrawable().setlevel(level); 
    break; 
   case batterymanager.battery_status_not_charging: 
    statusstring = "not charging"; 
    break; 
   case batterymanager.battery_status_full: 
    statusstring = "full"; 
    break; 
   } 
   string healthstring = ""; 
   switch (health) { 
   case batterymanager.battery_health_unknown: 
    healthstring = "unknown"; 
    break; 
   case batterymanager.battery_health_good: 
    healthstring = "good"; 
    break; 
   case batterymanager.battery_health_overheat: 
    healthstring = "overheat"; 
    break; 
   case batterymanager.battery_health_dead: 
    healthstring = "dead"; 
    break; 
   case batterymanager.battery_health_over_voltage: 
    healthstring = "voltage"; 
    break; 
   case batterymanager.battery_health_unspecified_failure: 
    healthstring = "unspecified failure"; 
    break; 
   } 
   string acstring = ""; 
   switch (plugged) { 
   case batterymanager.battery_plugged_ac: 
    acstring = "plugged ac"; 
    break; 
   case batterymanager.battery_plugged_usb: 
    acstring = "plugged usb"; 
    break; 
   } 
   log.i("cat", statusstring); 
   log.i("cat", healthstring); 
   log.i("cat", string.valueof(present)); 
   log.i("cat", string.valueof(level)); 
   log.i("cat", string.valueof(scale)); 
   log.i("cat", string.valueof(icon_small)); 
   log.i("cat", acstring); 
   log.i("cat", string.valueof(voltage)); 
   log.i("cat", string.valueof(temperature)); 
   log.i("cat", technology); 
   //要看看是不是我们要处理的消息 
   if (action.equals(intent.action_battery_changed)) {    
    //电池电量,数字 
    log.d("battery", "" + intent.getintextra("level", 0));     
    //电池最大容量 
    log.d("battery", "" + intent.getintextra("scale", 0));     
    //电池伏数 
    log.d("battery", "" + intent.getintextra("voltage", 0));     
    //电池温度 
    log.d("battery", "" + intent.getintextra("temperature", 0)); 
    //电池状态,返回是一个数字 
    // batterymanager.battery_status_charging 表示是充电状态 
    // batterymanager.battery_status_discharging 放电中 
    // batterymanager.battery_status_not_charging 未充电 
    // batterymanager.battery_status_full 电池满 
    log.d("battery", "ss" + intent.getintextra("status", batterymanager.battery_status_charging)); 
    //充电类型 batterymanager.battery_plugged_ac 表示是充电器,不是这个值,表示是 usb 
    log.d("battery", "" + intent.getintextra("plugged", 0)); 
    //电池健康情况,返回也是一个数字 
    //batterymanager.battery_health_good 良好 
    //batterymanager.battery_health_overheat 过热 
    //batterymanager.battery_health_dead 没电 
    //batterymanager.battery_health_over_voltage 过电压 
    //batterymanager.battery_health_unspecified_failure 未知错误 
    log.d("battery", "" + intent.getintextra("health", batterymanager.battery_health_unknown)); 
   } 
  } 
 }; 

这里真正处理图片变换的是以下几句话:

switch (status) { 
case batterymanager.battery_status_unknown: 
 statusstring = "unknown"; 
 break; 
case batterymanager.battery_status_charging: 
 statusstring = "charging"; 
 battery_image.setimageresource(r.drawable.stat_sys_battery_charge); 
 battery_image.getdrawable().setlevel(level); 
 break; 
case batterymanager.battery_status_discharging: 
 statusstring = "discharging"; 
 battery_image.setimageresource(r.drawable.stat_sys_battery); 
 battery_image.getdrawable().setlevel(level); 
 break; 
case batterymanager.battery_status_not_charging: 
 statusstring = "not charging"; 
 break; 
case batterymanager.battery_status_full: 
 statusstring = "full"; 
 break; 
}

4.在onresume方法中注册消息处理器

registerreceiver(mintentreceiver, mintentfilter); 

5.在onpause方法中注销

unregisterreceiver(mintentreceiver);

希望本文所述对大家的android程序设计有所帮助。