Android realizes the method of dynamic display of battery status display, power and charge _android

Source: Internet
Author: User

This article illustrates the Android implementation of a dynamic display of battery status, power and charge, and is a very common feature in the development of Android applications. Share for everyone to use for reference. The specific methods are as follows:

Use the battery on the Android system to listen and the battery picture of the system is as follows;

The introduction of the two XML files in the source code (which I can't find in the android2.2 jar package), source code path: C:\android-sdk-windows\platforms\android-8\data\res\ drawable), you will find the Stat_sys_battery.xml error, as shown in the following figure:

1. These graphs are introduced into my drawable directory, and then the pictures in Stat_sys_battery.xml are introduced to the path changes as follows:

<level-list xmlns:android= "Http://schemas.android.com/apk/res/android" > 
 <item android:maxlevel= "4" android:drawable= "@drawable/stat_sys_battery_0"/> 
 <item android:maxlevel= "android:drawable=" @ Drawable/stat_sys_battery_10 " 
 android:drawable=" @drawable "/> <item android:maxlevel="/stat_sys_ Battery_20 "/> 
 <item android:maxlevel=" android:drawable= "@drawable/stat_sys_battery_40" 
 /> <item android:maxlevel= "android:drawable=" "@drawable/stat_sys_battery_60"/> 
 <item android: Maxlevel= "android:drawable=" "@drawable/stat_sys_battery_80"/> 
 <item android:maxlevel= "MB" android: drawable= "@drawable/stat_sys_battery_100"/> 
</level-list>

2. Next is the action in the activity:

Create a Intentfilter

Private Intentfilter Mintentfilter; 

In the OnCreate method: add:

Battery related 
mintentfilter = new Intentfilter (); 
Mintentfilter.addaction (intent.action_battery_changed); 

3. Receiving broadcasts

Declares the message handling process private Broadcastreceiver Mintentreceiver = new Broadcastreceiver () {@Override public void onreceive (C 
   Ontext context, Intent Intent) {String action = intent.getaction (); 
   int status = Intent.getintextra ("status", 0); 
   int health = Intent.getintextra ("health", 0); 
   Boolean present = Intent.getbooleanextra ("Present", false); 
   int level = Intent.getintextra ("level", 0); 
   int scale = Intent.getintextra ("scale", 0); 
   int icon_small = Intent.getintextra ("Icon-small", 0); 
   int plugged = Intent.getintextra ("plugged", 0); 
   int voltage = Intent.getintextra ("voltage", 0); 
   int temperature = Intent.getintextra ("Temperature", 0); 
   String technology = Intent.getstringextra ("Technology"); 
   String statusstring = ""; 
    Switch (status) {case BatteryManager.BATTERY_STATUS_UNKNOWN:statusString = "UNKNOWN"; 
   Break 
    Case BatteryManager.BATTERY_STATUS_CHARGING:statusString = "charging"; Battery_image.setimageresource (R.Drawable.stat_sys_battery_charge); 
    Battery_image.getdrawable (). Setlevel (level); 
   Break 
    Case BatteryManager.BATTERY_STATUS_DISCHARGING:statusString = "discharging"; 
    Battery_image.setimageresource (R.drawable.stat_sys_battery); 
    Battery_image.getdrawable (). Setlevel (level); 
   Break 
    Case BatteryManager.BATTERY_STATUS_NOT_CHARGING:statusString = "not charging"; 
   Break 
    Case BatteryManager.BATTERY_STATUS_FULL:statusString = ' full '; 
   Break 
   } String healthstring = ""; 
    Switch (health) {Case BatteryManager.BATTERY_HEALTH_UNKNOWN:healthString = "UNKNOWN"; 
   Break 
    Case BatteryManager.BATTERY_HEALTH_GOOD:healthString = "good"; 
   Break 
    Case BatteryManager.BATTERY_HEALTH_OVERHEAT:healthString = "overheat"; 
   Break 
    Case BatteryManager.BATTERY_HEALTH_DEAD:healthString = "DEAD"; 
   Break 
    Case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:healthString = "voltage"; BReak; 
    Case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:healthString = "UNSPECIFIED failure"; 
   Break 
   } String acstring = ""; 
    Switch (plugged) {case BatteryManager.BATTERY_PLUGGED_AC:acString = "plugged AC"; 
   Break 
    Case BatteryManager.BATTERY_PLUGGED_USB:acString = "plugged USB"; 
   Break 
   } log.i ("Cat", statusstring); 
   LOG.I ("Cat", healthstring); 
   LOG.I ("Cat", string.valueof (present)); 
   LOG.I ("Cat", String.valueof (level)); 
   LOG.I ("Cat", string.valueof (scale)); 
   LOG.I ("Cat", String.valueof (Icon_small)); 
   LOG.I ("Cat", acstring); 
   LOG.I ("Cat", string.valueof (voltage)); 
   LOG.I ("Cat", string.valueof (temperature)); 
   LOG.I ("Cat", technology); To see if we're going to process the message if (Action.equals (intent.action_battery_changed)) {//battery charge, Digital log.d ("BATTERY", "" + inte     
    Nt.getintextra ("level", 0));     
    Maximum battery capacity log.d ("Battery", "" + Intent.getintextra ("scale", 0)); Battery Count Log.d ("Battery "," "+ Intent.getintextra (" voltage ", 0)); 
    Battery temperature Log.d ("Battery", "" + Intent.getintextra ("Temperature", 0)); Battery state, the return is a number//batterymanager.battery_status_charging representation is charged state//batterymanager.battery_status_discharging discharge Medium//Batterymanager.battery_status_not_charging not charged//Batterymanager.battery_status_full battery full Log.d ("Batter 
    Y "," ss "+ Intent.getintextra (" status ", batterymanager.battery_status_charging)); Charging type Batterymanager.battery_plugged_ac is the charger, not this value, which means USB log.d ("BATTERY", "" + Intent.getintextra ("plugged", 0)) 
    ; Battery health condition, return is also a digital//batterymanager.battery_health_good good//batterymanager.battery_health_overheat overheating//Bat Terymanager.battery_health_dead not//batterymanager.battery_health_over_voltage over voltage//BatteryManager.BATTERY_HEA Lth_unspecified_failure Unknown error log.d ("Battery", "" + Intent.getintextra ("Health", Batterymanager.battery_health_unknow 
   N)); 

 } 
  } 
 };

Here the real processing picture transforms is the following words:

Switch (status) {case 
batterymanager.battery_status_unknown: 
 statusstring = "UNKNOWN"; 
 break; 
Case batterymanager.battery_status_charging: 
 statusstring = "charging"; 
 Battery_image.setimageresource (r.drawable.stat_sys_battery_charge); 
 Battery_image.getdrawable (). Setlevel (level); 
 break; 
Case batterymanager.battery_status_discharging: 
 statusstring = "discharging"; 
 Battery_image.setimageresource (r.drawable.stat_sys_battery); 
 Battery_image.getdrawable (). Setlevel (level); 
 break; 
Case batterymanager.battery_status_not_charging: 
 statusstring = "not charging"; 
 break; 
Case Batterymanager.battery_status_full: 
 statusstring = "full"; 
 break; 


4. Registering the Message processor in the Onresume method

Registerreceiver (Mintentreceiver, Mintentfilter); 

5. Log off in the OnPause method

Unregisterreceiver (Mintentreceiver);

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

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