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

前端技术 2023/09/01 Android

本文实例讲述了Android实现侦听电池状态显示、电量及充电动态显示的方法,是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中的图片引入路径更改,如下:

<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程序设计有所帮助。

本文地址:https://www.stayed.cn/item/2651

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。