当前位置: 编程技术>移动开发
Android实现侦听电池状态显示、电量及充电动态显示的方法
来源: 互联网 发布时间:2014-10-26
本文导语: 本文实例讲述了Android实现侦听电池状态显示、电量及充电动态显示的方法,是Android应用程序开发中非常常用的重要功能。分享给大家供大家参考之用。具体方法如下: 使用android系统的电池侦听和系统的电池图片如下; 引入...
本文实例讲述了Android实现侦听电池状态显示、电量及充电动态显示的方法,是Android应用程序开发中非常常用的重要功能。分享给大家供大家参考之用。具体方法如下:
使用android系统的电池侦听和系统的电池图片如下;
引入源代码中的两个xml文件(这里我在android2.2的jar包中找不到,所以才引入,源代码路径:C:android-sdk-windowsplatformsandroid-8dataresdrawable),之后会发现stat_sys_battery.xml报错,如下图所示:
1.这些图引入到我的drawable目录下,然后将stat_sys_battery.xml中的图片引入路径更改,如下:
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程序设计有所帮助。