Android programming method for detecting the current power status
This article describes how to detect the current power status using Android programming. We will share this with you for your reference. The details are as follows:
The current power status is detected:
IntentFilter mIntentFilter = new IntentFilter ();
MIntentFilter. addAction (Intent. ACTION_BATTERY_CHANGED );
RegisterReceiver (mIntentReceiver, mIntentFilter );
// Declare the Message Processing Process
Private BroadcastReceiver mIntentReceiver = new BroadcastReceiver (){
@ Override
Public void onReceive (Context context, Intent intent ){
String action = intent. getAction ();
// Check whether the message is the one we want to process.
If (action. equals (Intent. ACTION_BATTERY_CHANGED )){
// Battery power, number
Log. d ("Battery", "" + intent. getIntExtra ("level", 0 ));
// Maximum battery capacity
Log. d ("Battery", "" + intent. getIntExtra ("scale", 0 ));
// Batteries
Log. d ("Battery", "" + intent. getIntExtra ("voltage", 0 ));
// Battery temperature
Log. d ("Battery", "" + intent. getIntExtra ("temperature", 0 ));
// Battery status. A number is returned.
// BatteryManager. BATTERY_STATUS_CHARGING indicates the charging status.
// BatteryManager. BATTERY_STATUS_DISCHARGING under discharge
// BatteryManager. BATTERY_STATUS_NOT_CHARGING is not charged
// BatteryManager. BATTERY_STATUS_FULL battery is full
Log. d ("Battery", "" + intent. getIntExtra ("status", BatteryManager. BATTERY_STATUS_UNKNOWN ));
// BatteryManager. BATTERY_PLUGGED_AC indicates the charger, not the value, indicating the USB
Log. d ("Battery", "" + intent. getIntExtra ("plugged", 0 ));
// Battery health condition. A number is returned.
// BatteryManager. BATTERY_HEALTH_GOOD
// BatteryManager. BATTERY_HEALTH_OVERHEAT is overheated.
// BatteryManager. BATTERY_HEALTH_DEAD no electricity
// BatteryManager. BATTERY_HEALTH_OVER_VOLTAGE overvoltage
// BatteryManager. BATTERY_HEALTH_UNSPECIFIED_FAILURE unknown error
Log. d ("Battery", "" + intent. getIntExtra ("health", BatteryManager. BATTERY_HEALTH_UNKNOWN ));
}
}
};
I hope this article will help you with Android program design.