Android programming to monitor battery status sample _android

Source: Internet
Author: User

This article describes the implementation of Android programming to monitor battery status. Share to everyone for your reference, specific as follows:

Recently in the development of a GPS-related project, because it involves the use of GPs, it is well known that GPS is quite power consumption, so think how to know the current electricity, and when the electricity reaches a lower limit, timely reminder to the user, in accordance with the situation to close the GPS, save electricity, in case of telephone emergency, Later, look at the information, see the API, finally found a way, how to monitor the electricity, according to the changes in electricity to obtain the current amount of electricity, and according to other state integrated mobile phone management, to achieve the optimal management status.

Here's the code:

Private Broadcastreceiver Mbatinforeceiver = new Broadcastreceiver () {int intlevel = 0;
    int intscale = 0;
      public void OnReceive (context context, Intent Intent) {String action = intent.getaction (); * * If the ACTION captured is action_battery_changed, run onbatteryinforeceiver ()/if (Intent.action_battery_chan
        Ged.equals (Action)) {intlevel = Intent.getintextra ("level", 0);
        Intscale = Intent.getintextra ("scale", 100);
        Battery Volt number log.d ("Battery V", "" + Intent.getintextra ("voltage", 0));
        Battery temperature Log.d ("Battery T", "" + Intent.getintextra ("Temperature", 0));
        Batteryv = "Current voltage is:" + Intent.getintextra ("voltage", 0);
        Batteryt = "Current temperature is:" + Intent.getintextra ("Temperature", 0); Switch (Intent.getintextra ("status", Batterymanager.battery_status_unknown)) {case Batterymanager.bat
          Tery_status_charging:batterystatus = "Charge State";
        Break Case BatterymanAger.
          Battery_status_discharging:batterystatus = "discharge state";
        Break
          Case BatteryManager.BATTERY_STATUS_NOT_CHARGING:BatteryStatus = "not charged";
        Break
          Case BatteryManager.BATTERY_STATUS_FULL:BatteryStatus = "full power";
        Break
          Case BatteryManager.BATTERY_STATUS_UNKNOWN:BatteryStatus = "not known state";
        Break Switch (Intent.getintextra ("plugged", Batterymanager.battery_plugged_ac)) {case Batterymana Ger.
          Battery_plugged_ac:batterystatus2 = "AC charge";
        Break
          Case BATTERYMANAGER.BATTERY_PLUGGED_USB:BATTERYSTATUS2 = "USB charging";
        Break Switch (Intent.getintextra ("Health", Batterymanager.battery_health_unknown)) {case Batterym Anager.
          Battery_health_unknown:batterytemp = "Unknown error";
        Break
       Case BatteryManager.BATTERY_HEALTH_GOOD:BatteryTemp = "in good condition";   Break
          Case BatteryManager.BATTERY_HEALTH_DEAD:BatteryTemp = "battery without electricity";
        Break
          Case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:BatteryTemp = "battery voltage is too high";
        Break
          Case BatteryManager.BATTERY_HEALTH_OVERHEAT:BatteryTemp = "battery overheating";
        Break

 }
      }
    }
};

This information is to monitor the broadcast, according to the broadcast information to judge, if it is the electricity change broadcast, then according to this information, updates the data, thus carries on the analysis!

For more information on Android-related content readers can view the site topics: "Android Database Operating skills summary", "Android programming activity Operation Skills Summary", "Android File Operation skills Summary", " Android programming development of the SD card operation method Summary, "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android View tips Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

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.