Android Battery Status Monitor

Source: Internet
Author: User

If you want to monitor battery status changes, you need to register dynamically: Android.intent.action.BATTERY_CHANGED, after receiving the action, you can obtain the information you need according to the corresponding key. For more information, refer to the Batterychangedreceiver class in the following example

Package Com.example.charginganimation;import;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 (); overrideprotected 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 ;} @Overrideprotected void OnDestroy () {//TODO auto-generated Method Stubunregisterreceiver (receiver); Super.ondestroy () ;} Class Batterychangedreceiver extends Broadcastreceiver{@Overridepublic void OnReceive (context context, Intent Intent) {//TODO auto-generated method stubfinal String action = i Ntent.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 int. = Intent.getintextra (Batterymanager.extra_health,-1); BatteryManager.BATTERY_HEALTH_COLD:System.out.println ("Battery_health_cold"); Break;case 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"); Default:break;} The current power of the battery, which is between 0 and Extra_scale int level = Intent.getintextra (Batterymanager.extra_level,-1);//The maximum value of the battery charge int scale = inte Nt.getintextra (Batterymanager.extra_scale,-1);//Where the current phone is using the power 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 PortSystem.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 of 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"); Default:break;} Technology for battery use. For example, for lithium battery is li-ionstring 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 power to a normal System.out.println (" Batterychangedreceiver action_battery_okay---");}}}

Android Battery Status Monitor

Related Article

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: 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.