Recently, a GPS-related project has been developed, because it involves the use of GPS. As we all know, GPS consumes a lot of power, so I want to know how to know the current power, when the power usage reaches a lower limit, the user is reminded in time to disable GPS as needed to save power for urgent calls. Later, the user checked the information and looked at the API and finally found a method, how to monitor the power, how to obtain the current power according to the power change, and manage the Mobile Phone Based on other statuses to achieve optimal management.
Below isCode:
Code
Private Broadcastreceiver mbatinforeceiver = New Broadcastreceiver (){
Int Intlevel = 0 ;
Int Intscale = 0 ;
Public Void Onreceive (context, intent ){
String action = Intent. getaction ();
/*
* If the captured action is action_battery_changed, run onbatteryinforeceiver ()
*/
If (Intent. action_battery_changed.equals (Action )){
Intlevel = Intent. getintextra ( " Level " , 0 );
Intscale = Intent. getintextra ( " Scale " , 100 );
// Batteries
Log. D ( " Battery v " , "" + Intent. getintextra ( " Voltage " , 0 ));
// Battery Temperature
Log. D ( " Battery t " , "" + Intent. getintextra ( " Temperature " , 0 ));
Batteryv = " Current voltage: " + Intent. getintextra ( " Voltage " , 0 );
Batteryt = " Current temperature: " + Intent. getintextra ( " Temperature " , 0 );
Switch (Intent. getintextra ( " Status " ,
Batterymanager. battery_status_unknown )){
Case Batterymanager. battery_status_charging:
Batterystatus = " Charging status " ;
Break ;
Case Batterymanager. battery_status_discharging:
Batterystatus = " Discharge status " ;
Break ;
Case Batterymanager. battery_status_not_charging:
Batterystatus = " Not charged " ;
Break ;
Case Batterymanager. battery_status_full:
Batterystatus = " Fully charged " ;
Break ;
Case Batterymanager. battery_status_unknown:
Batterystatus = " Unknown Status " ;
Break ;
}
Switch (Intent. getintextra ( " Plugged " ,
Batterymanager. battery_plugged_ac )){
Case Batterymanager. battery_plugged_ac:
Batterystatus2 = " AC charging " ;
Break ;
Case Batterymanager. battery_plugged_usb:
Batterystatus2 = " USB charging " ;
Break ;
}
Switch (Intent. getintextra ( " Health " ,
Batterymanager. battery_health_unknown )){
Case Batterymanager. battery_health_unknown:
Batterytemp = " Unknown error " ;
Break ;
Case Batterymanager. battery_health_good:
Batterytemp = " Good status " ;
Break ;
Case Batterymanager. battery_health_dead:
Batterytemp = " No battery " ;
Break ;
Case Batterymanager. battery_health_over_voltage:
Batterytemp = " High battery voltage " ;
Break ;
Case Batterymanager. battery_health_overheat:
Batterytemp = " The battery is overheated. " ;
Break ;
}
}
}
};
This information is used to listen to the broadcast. It is determined based on the broadcast information. If it is a broadcast with a change in power, it is used to update the data for analysis!