Android Monitor Battery Status Instance code _android

Source: Internet
Author: User
Tags stub

If you want to monitor the status of the battery changes, need to register dynamically: Android.intent.action.BATTERY_CHANGED, after receiving the action can be based on the corresponding key to get the information you need, More detailed information can refer to the Batterychangedreceiver class in the following example

The specific code looks like this:

Package com.example.charginganimation;
Import android.app.Activity;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.os.BatteryManager;
Import Android.os.Bundle;
Import Android.view.View; public class Mainactivity extends activity {private customcliploading CCL; private Batterychangedreceiver receiver = new
Batterychangedreceiver (); @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.main);
Registerreceiver (receiver, GetFilter ()); Private Intentfilter GetFilter () {intentfilter filter = new Intentfilter (); Filter.addaction (Intent.action_battery_
CHANGED);
Filter.addaction (Intent.action_battery_low);
Filter.addaction (Intent.action_battery_okay);
return filter; @Override protected void OnDestroy () {//TODO auto-generated method stub unregisterreceiver (receiver); Super.ondestroy (
); } class BatterychangedrecEiver extends Broadcastreceiver {@Override public void onreceive (context context, Intent Intent) {//TODO auto-generated
Method Stub final String action = Intent.getaction (); if (Action.equalsignorecase (intent.action_battery_changed)) {System.out. println ("Batterychangedreceiver BATTERY_
Changed_action---");
Current battery voltage int voltage = Intent.getintextra (Batterymanager.extra_voltage,-1);
Battery health status int health = intent. Getintextra (Batterymanager.extra_health,-1); Switch (health) {case BATTERYMANAGER.BATTERY_HEALTH_COLD:SYSTEM.OUT.PRINTLN ("Battery_health_cold");
BatteryManager.BATTERY_HEALTH_DEAD:System.out.println ("Battery_health_dead");
Break
Case BatteryManager.BATTERY_HEALTH_GOOD:System.out.println ("Battery_health_good");
Break
Case BatteryManager.BATTERY_HEALTH_OVERHEAT:System.out.println ("battery_health_overheat");
Break
Case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:System.out.println ("Battery_health_cold");
Break Case Batterymanager.battery_health_unknowN:system.out.println ("Battery_health_unknown");
Break Case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:System.out.println ("Battery_health_unspecified_failure")
;
Break
Default:break;
}//battery current power, which is between 0 and Extra_scale int level = Intent.getintextra (Batterymanager.extra_level,-1);
Maximum battery charge int scale = Intent.getintextra (Batterymanager.extra_scale,-1);
Where the current phone is using the power of the int pluged = Intent.getintextra (batterymanager.extra_plugged,-1); Switch (pluged) {case BATTERYMANAGER.BATTERY_PLUGGED_AC://Power supply is AC charger.[
Should refer to the charger] System.out.println ("Battery_plugged_ac");
Break
Case BATTERYMANAGER.BATTERY_PLUGGED_USB://Power supply is USB port System.out.println ("Battery_plugged_usb");
Break
Default:break;
int status = Intent. Getintextra (Batterymanager.extra_status,-1);
Switch (status) {case batterymanager.battery_status_charging://Is charging System.out.println ("battery_status_charging");
Break Case BatteryManager.BATTERY_STATUS_DISCHARGING:System.out.println ("battery_status_discharging");
Break
Case Batterymanager.battery_status_full://Full System.out.println ("Battery_status_full");
Break
Case batterymanager.battery_status_not_charging://No Charge System.out.println ("battery_status_not_charging");
Break
Case Batterymanager.battery_status_unknown://Unknown State System.out.println ("Battery_status_unknown");
Break
Default:break; }//battery-used technology.
For example, the lithium battery is li-ion String technology = intent. Getstringextra (batterymanager.extra_technology);
Current battery temperature int temperature = Intent.getintextra (Batterymanager.extra_temperature,-1); SYSTEM.OUT.PRINTLN ("voltage =" + voltage + "Technology =" + Technology + "Temperature =" + temperature + "level =" + level + "scale =" + scale);} else if (Action.equalsignorecase (Intent.action_battery_low)) {//indicates that the current battery charge is low System.out. println ("
Batterychangedreceiver action_battery_low---"); else if (Action.equalsignorecase (Intent.action_battery_okay)) {//indicates that the current battery has been restored from low battery to normal System.out. println ("
Batterychangedreceiver action_battery_okay---"); }
}
}
} 

Get apk and version information

private int Getvercode (context context) {
int vercode =-1;
try {
Vercode = Context.getpackagemanager (). Getpackageinfo (
"Com.example.charginganimation", 0). Versioncode;
} catch (Namenotfoundexception e) {
e.printstacktrace ();
}
return vercode;
}
private string Getvername (context context) {
String vername = "";
try {
vername = Context.getpackagemanager (). Getpackageinfo (
"Com.example.charginganimation", 0). Versionname;
} catch (Namenotfoundexception e) {
e.printstacktrace ();
}
return vername;
}

The above code content is small to introduce the Android Monitor battery status of the example code, this article is not good to write, please forgive me, there are good comments welcome to small series of messages, I will promptly communicate to you, thank you all the time has been on the cloud Habitat Community support site!

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.