Android Battery status Source Code

Source: Internet
Author: User

/**
* Declare an intentfilter object first
*/
Private intentfilter mintentfilter;
Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mintentfilter = new intentfilter ();
Mintentfilter. addaction (intent. action_battery_changed );
}
 
Protected void onresume ()
{
Super. onresume ();
// Register the message processor
Registerreceiver (mintentreceiver, mintentfilter );
}
 
// Declare the Message Processing Process
Private broadcastreceiver mintentreceiver = new broadcastreceiver (){
@ Override
Public void onreceive (context, 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 ));
}
}
};

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.