Android编程实现检测当前电源状态的方法
本文实例讲述了Android编程实现检测当前电源状态的方法。分享给大家供大家参考,具体如下:
检测到现在在电源状态:
IntentFiltermIntentFilter=newIntentFilter(); mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); registerReceiver(mIntentReceiver,mIntentFilter); //声明消息处理过程 privateBroadcastReceivermIntentReceiver=newBroadcastReceiver(){ @Override publicvoidonReceive(Contextcontext,Intentintent){ Stringaction=intent.getAction(); //要看看是不是我们要处理的消息 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",""+intent.getIntExtra("status",BatteryManager.BATTERY_STATUS_UNKNOWN)); //充电类型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)); } } };
希望本文所述对大家Android程序设计有所帮助。