MSM8909+ANDROID5.1.1 's BSP Development---battery management 2---Batteryinfo.java
First to borrow the MTK on the battery management frame diagram
Figure 1
Test command via phone: *#* #4636 #*#* can eject testing interface
Figure 2
Select battery information to enter:
Figure 3
When we plug in USB or DC, thePower plug is displayed Unknown
The bottom is judged as the unknown type, this problem should be checked clearly
The code for Batteryinfo corresponds to Packages\apps\settings\src\com\android\settings\batteryinfo.java
1. \packages\apps\settings\androidmanifest.xml
<activityandroid:name= "Batteryinfo" android:label= "@string/battery_info_label" android:taskaffinity= " Com.android.settings " android:parentactivityname=" Settings "> <intent-filter> < Actionandroid:name= "Android.intent.action.MAIN"/> <categoryandroid:name= " Android.intent.category.DEVELOPMENT_PREFERENCE "/> <categoryandroid:name=" Android.intent.category.VOICE_LAUNCH "/> </intent-filter> </activity>
(1) Android:label
Android:label= "@string/battery_info_label", the label represents the tag, @ is the reference, which represents the value Battery_info_label this key value from the string file.
The corresponding values in \packages\apps\settings\res\values\ Strings.xml are as follows:
<!--the title of the activity to Seebattery info. -
<stringname= "Battery_info_label" >battery info</string>
(2) Android:taskaffinity
Android:taskaffinity= "Com.android.settings" indicates that batteryinfo activity wants to enter com.android.settings this task.
The attribution of activity, which is the task in which the activity should be, the adsorption relationship between activity and task. We know that, in general, in the same application, the activity that is initiated is in the same task, and they spend their life cycle in that task, which is a good example of mindedness.
So why did the activity that we created go into this task? Will they be transferred to other tasks? If you go to other tasks, what kind of task will they go to?
The key to solving these problems is the Taskaffinity property of each activity.
Each activity has a taskaffinity attribute, which indicates the task it wants to enter. If an activity does not explicitly indicate the activity's taskaffinity, then its attribute is equal to the taskaffinity specified by application, and if application is not specified, Then the value of the taskaffinity is equal to the package name. The task also has its own affinity property, whose value equals the value of the taskaffinity of its root activity.
2. Batteryinfo.java Code Analysis
2.1 OnCreate ()
@Override public void OnCreate (Bundle icicle) { super.oncreate (icicle); Setcontentview (r.layout.battery_info); Create the intentfilter that'll be used to listen //To battery status broadcasts Mintentfilter = new intentf Ilter (); Mintentfilter.addaction (intent.action_battery_changed); }
(1) R.layout.battery_info
Its value is defined in Packages\apps\settings\res\layout\battery_info.xml
(2) Create an object of the Intentfilter class Mintentfilter
Used to monitor battery status broadcasts.
(3) mintentfilter.addaction (intent.action_battery_changed);
For this intentfilter, add a action_battery_changed action, which means that when other activity, or service, sends intent containing action_battery_changed, Batteryinfo action to update battery status
2.2 Onresume ()
public void Onresume () { super.onresume (); Mstatus = (TextView) Findviewbyid (r.id.status); MPower = (TextView) Findviewbyid (r.id.power); Mlevel = (TextView) Findviewbyid (r.id.level); Mscale = (TextView) Findviewbyid (R.id.scale); MHealth = (TextView) Findviewbyid (r.id.health); Mtechnology = (TextView) Findviewbyid (r.id.technology); Mvoltage = (TextView) Findviewbyid (r.id.voltage); Mtemperature = (TextView) Findviewbyid (r.id.temperature); Muptime = (TextView) Findviewbyid (r.id.uptime); Get awake time plugged in and on battery mbatterystats =ibatterystats.stub.asinterface ( Servicemanager.getservice ( batterystats.service_name)); Mscreenstats = IPowerManager.Stub.asInterface (Servicemanager.getservice (Power_service)); Mhandler.sendemptymessagedelayed (Event_tick, n); Registerreceiver (Mintentreceiver, mintentfilter); }
Through Registerreceiver (Mintentreceiver,mintentfilter) in the Onresume () method, register a receiver and eventually receive a value of Action_ in the OnReceive () method Battery_changed action, Batteryinfo This activity to update the battery status information.
(1) Create a handler
Mhandler.sendemptymessagedelayed (event_tick,3000); Use to Handler, which is created as follows:
Private Handler Mhandler = new Handler () { @Override public void Handlemessage (Message msg) { switch (msg.wha T) {case Event_tick: updatebatterystats (); Sendemptymessagedelayed (Event_tick, +); Break;}} ;
Rewrite Handlemessage () is used to process the message Event_tick, after receiving this message, call Updatebatterystats () to update the battery status and send a Event_tick message after 1000ms.
(2) Register a listener
/** *listens for Intent broadcasts */private intentfilter mintentfilter; Private Broadcastreceiver Mintentreceiver = new Broadcastreceiver () {@Override public void onreceive (Context Context, Intent Intent) {String action = intent.getaction (); if (Action.equals (intent.action_battery_changed)) {int Plugtype =intent.getintextra ("plugged", 0); Mlevel.settext ("" +intent.getintextra ("level", 0)); Mscale.settext ("" +intent.getintextra ("scale", 0)); Mvoltage.settext ("" +intent.getintextra ("voltage", 0) + "" +getstring (R.string.battery_info_voltag E_units)); Mtemperature.settext ("+tenthstofixedstring" (Intent.getintextra ("Temperature", 0)) +getstring (R.str Ing.battery_info_temperature_units)); Mtechnology.settext ("+intent.getstringextra" ("Technology")); Mstatus.settext (Utils.getbatterystaTus (getresources (), intent)); Switch (plugtype) {case 0:mpower.settext (getString (r.string.battery_info_power_ Unplugged)); Break Case BatteryManager.BATTERY_PLUGGED_AC:mPower.setText (getString (R.STRING.BATTERY_INFO_POWER_AC)); Break Case BatteryManager.BATTERY_PLUGGED_USB:mPower.setText (getString (R.STRING.BATTERY_INFO_POWER_USB)); Break CaseBatteryManager.BATTERY_PLUGGED_WIRELESS:mPower.setText (GetString (r.string.battery_info_power_wi reless)); Break Case (batterymanager.battery_plugged_ac| BATTERYMANAGER.BATTERY_PLUGGED_USB): Mpower.settext (getString (R.STRING.BATTERY_INFO_POWER_AC_USB)); Break Default Mpower.settext (getString (R.string.battery_info_power_unknown)); Break } int Health =intent.getintextra ("Health", Batterymanager.battery_health_unknown); String healthstring; if (health ==batterymanager.battery_health_good) {healthstring =getstring (r.string.battery_info_health _good); } else if (health ==batterymanager.battery_health_overheat) {healthstring =getstring (r.string.battery_i Nfo_health_overheat); } else if (health ==batterymanager.battery_health_dead) {healthstring =getstring (r.string.battery_info _health_dead); } else if (health ==batterymanager.battery_health_over_voltage) {healthstring =getstring (R.string.batt Ery_info_health_over_voltage); } else if (health ==batterymanager.battery_health_unspecified_failure) {HeaLthstring =getstring (r.string.battery_info_health_unspecified_failure); } else if (health ==batterymanager.battery_health_cold) {healthstring =getstring (r.string.battery_info _health_cold); } else {healthstring =getstring (r.string.battery_info_health_unknown); } mhealth.settext (healthstring); } } };
1) Update of Battery status information
Some of the battery status is special:
Mstatus.settext (Utils.getbatterystatus (getresources (), intent));
See the implementation of Packages\apps\settings\src\com\android\settings\utils.java\getbatterystatus ():
public static Stringgetbatterystatus (Resources res, Intent batterychangedintent) {final Intent Intent = Batterychan Gedintent; int plugtype = Intent.getintextra (batterymanager.extra_plugged, 0); int status = Intent.getintextra (Batterymanager.extra_status, Batterymanager.battery_status_unknown); String statusstring; if (status = = batterymanager.battery_status_charging) {int resId; if (Plugtype = = Batterymanager.battery_plugged_ac) {resId =r.string.battery_info_status_charging_ac; } else if (Plugtype = = Batterymanager.battery_plugged_usb) {resId =r.string.battery_info_status_char GING_USB; } else if (Plugtype = = batterymanager.battery_plugged_wireless) {resId =r.string.battery_info_status_charg ing_wireless; } else {resId =r.string.battery_info_status_charging; } statusstring = Res.getstring (resId); } else if (status ==batterymanager.battery_status_discharging) {statusstring = res.getstring (r.string.battery_info_st atus_discharging); } else if (status = = batterymanager.battery_status_not_charging) {statusstring = res.getstring (r.string.battery _info_status_not_charging); } else if (status = = Batterymanager.battery_status_full) {statusstring = res.getstring (r.string.battery_info_st Atus_full); } else {statusstring = res.getstring (R.string.battery_info_status_unknown); } return statusstring;}
From above, Plugtype is unknown, so from \packages\apps\settings\res\values\strings.xml's Battery_info_status_ Charging gets to the charging state is Charing, as follows:
<stringname= "Battery_info_status_charging" >Charging</string>
2) How to get the battery information
public void OnReceive (Context context,intent Intent) { String action = intent.getaction (); if (Action.equals (intent.action_battery_changed)) { int plugtype =intent.getintextra ("plugged", 0);
This is a piece of code above, received broadcast, and where the value of intent ACTION is action_battery_changed when the value of the plugged is read from intent, this intent is broadcast by the Batteryservice.
2.3 OnPause ()
@Override public void OnPause () { super.onpause (); Mhandler.removemessages (Event_tick); SLOG.E (TAG, "Batteryinfo--->onpause ()"); We is no longer on the screen stop the observers Unregisterreceiver (Mintentreceiver);}
It's simpler to delete Event_tick messages and logoff listeners Mintentreceiver
MSM8909+ANDROID5.1.1 's BSP Development---battery management 2---Batteryinfo.java