Android monitors Battery status

Source: Internet
Author: User

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!

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.