Android-Battery Status

Source: Internet
Author: User

In order to solve the problem of battery icon, incidentally looked at the battery information access method; I wrote a little chestnut to verify the effect.

Battery information, generally in the batterymanager inside, the information is sent by radio. We need a broadcast receiver to update the information

Register a broadcast receiver, receive intent.action_battery_changed , read the desired battery information from the Intent

Like batterymanager.extra_status batterymanager.battery_status_charging and so on.

In Android 5.1, the battery information can be found in the Settings-battery

1  Packagecom.example.chargingwatching;2 3 Importandroid.app.Activity;4 ImportAndroid.content.BroadcastReceiver;5 ImportAndroid.content.Context;6 Importandroid.content.Intent;7 ImportAndroid.content.IntentFilter;8 ImportAndroid.os.BatteryManager;9 ImportAndroid.os.Bundle;Ten ImportAndroid.view.WindowManager; One ImportAndroid.widget.TextView; A  -  Public classMainactivityextendsActivity { -     PrivateTextView chargestatus; the     PrivateTextView leveldigit; -     PrivateTextView Percentview; -     Private intRustlevel = 0;/*Battery level*/ -     Private intWindowWidth = 0;  +     PrivateWindowManager Mwindowmanager; -  +     PrivateBroadcastreceiver Mbatterystatureceiver =NewBroadcastreceiver () { A         intStatus//Current battery Status at         intPlugtype; -          Public voidOnReceive (Context context, Intent Intent) { -  -Rustlevel = (int) (100f -* Intent.getintextra (batterymanager.extra_level, 0) -/Intent.getintextra (Batterymanager.extra_scale, 100)); in  -Leveldigit.settext ("" + rustlevel + "%"); to              +Percentview.setwidth ((int) (WindowWidth * rustlevel/100));  -Status =Intent.getintextra (Batterymanager.extra_status, the batterymanager.battery_status_unknown); *Plugtype = Intent.getintextra (batterymanager.extra_plugged, 0); $ Panax Notoginseng             if(Status = =batterymanager.battery_status_charging) { -                 if(Plugtype = =Batterymanager.battery_plugged_ac) { the Chargestatus.settext (r.string.ac_charging); +}Else if(Plugtype = =Batterymanager.battery_plugged_usb) { A Chargestatus.settext (r.string.usb_charging); the                 } +}Else if(Status = =batterymanager.battery_status_full) { -                 if(Plugtype = =Batterymanager.battery_plugged_ac) { $ Chargestatus.settext (R.STRING.FULL_AC); $}Else if(Plugtype = =Batterymanager.battery_plugged_usb) { - Chargestatus.settext (R.STRING.FULL_USB); -                 } the}Else if(Status = =batterymanager.battery_status_not_charging) { - Chargestatus.settext (r.string.uncharge);Wuyi}Else if(Status = =batterymanager.battery_status_discharging) { the Chargestatus.settext (r.string.discharging); -}Else { Wu Chargestatus.settext (r.string.unknown); -             } About         } $     }; -  -@SuppressWarnings ("Deprecation") - @Override A      Public voidonCreate (Bundle savedinstancestate) { +         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.watch_charging); -Chargestatus =(TextView) Findviewbyid (r.id.tv_charge); $Leveldigit =(TextView) Findviewbyid (r.id.tv_battery_level_digit); thePercentview =(TextView) Findviewbyid (R.id.percent_view); the  theMwindowmanager = This. Getwindowmanager (); theWindowWidth =Mwindowmanager.getdefaultdisplay (). getwidth (); -  inIntentfilter filter =NewIntentfilter ();  the filter.addaction (intent.action_battery_changed); the Registerreceiver (mbatterystatureceiver, filter); About     } the}

The following is a simple layout file

The body is linearlayout, put a textview to show the battery status, a textview to show the power percentage

A crude power strip, TextView to impersonate.

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6 7     <TextView8         Android:id= "@+id/tv_charge"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" One Android:text= "Detecting ..."  A android:textsize= "20SP" -         /> -  the     <LinearLayout -         Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - android:orientation= "Horizontal" > +  -         <TextView +             Android:id= "@+id/tv_battery_level" A Android:layout_width= "Wrap_content" at Android:layout_height= "Match_parent" - Android:text= "@string/battery_level"  - android:textsize= "20SP"/> -  -         <TextView -             Android:id= "@+id/tv_battery_level_digit" in Android:layout_width= "Match_parent" - Android:layout_height= "Match_parent"  to android:textsize= "20SP"/> +     </LinearLayout> -  the     <TextView *         Android:id= "@+id/percent_view" $ Android:layout_height= "20DP"Panax Notoginseng Android:layout_width= "Wrap_content" - Android:background= "#00AA00" the         /> + </LinearLayout>

Android-Battery Status

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.