Low Power Process

Source: Internet
Author: User

Reproduced on:

Http://www.th7.cn/Program/Android/201212/114873.shtml

We all know that many functions of Android power are in the service, so are low power, in batteryservice. in Java, we can use the JNI layer (of course, the power of the JNI layer is also received from the lower-layer kernel. We will not analyze it too much here. If you are interested, you can take a look at it) obtain the batterylevel of the machine's battery. We also know that when the cell phone's battery is low, it will warn you that some of them will send a sound reminder, and will automatically shut down when it is too low!
The following describes the process:

In batteryservice. Java, when we obtain the current power from JNI, we can make a judgment in update:
Final Boolean sendbatterylow =! Plugged

& Mbatterystatus! = Batterymanager. battery_status_unknown

& Mbatterylevel <= mlowbatterywarninglevel

& (Oldplugged | mlastbatterylevel> mlowbatterywarninglevel );


If the current power is less than the warning power (in config. <integer name = "config_lowbatterywarninglevel"> 15 </Integer> in XML), a low power usage prompt is displayed, or the power usage is 0 (of course, this error may also occur when it is 5%) automatically shut down.

Check whether the power consumption is low:

If (sendbatterylow ){

Msentlowbatterybroadcast = true;

Statusintent. setaction (intent. action_battery_low );

Mcontext. sendbroadcast (statusintent );
}
If the power is low, a broadcast is sent.
This code automatically shuts down because the power consumption is too low:
Private final void shutdownifnopower (){

If (mbatterylevel = 0 &&! Ispowered () & activitymanagernative. issystemready ()){

Intent intent = new intent (intent. action_request_shutdown );

Intent. putextra (intent. extra_key_confirm, false );

Intent. setflags (intent. flag_activity_new_task );

Mcontext. startactivity (intent );

}

}

In statusbarpolicy. Java, broadcastreceiver mintentreceiver = new broadcastreceiver (){
@ Override
Public void onreceive (context, intent ){
String action = intent. getaction ();
.........

When action_battery_low is received:

Else if (action. Equals (intent. action_battery_low )){

Onbatterylow (intent );

}
The onbatterylow (intent) method will be available in statusbarpolicy. Java for processing:
Private void onbatterylow (intent ){
If (show_low_battery_warning ){
If (false ){
Slog. D (TAG, "mphonestate =" + mphonestate
+ "Mlowbatterydialog =" + mlowbatterydialog
+ "Mbatteryshowlowonendcall =" + mbatteryshowlowonendcall );
}

If (show_battery_warnings_in_call | mphonestate = telephonymanager. call_state_idle ){
Showlowbatterywarning ();
} Else {
Mbatteryshowlowonendcall = true;
}
}
}
Then the dialog of the Low-Power reminder is displayed:
Private void showlowbatterywarning (){
Closelastbatteryview ();

// Show exact battery level.
Charsequence leveltext = mcontext. getstring (
R. String. battery_low_percent_format, mbatterylevel );

If (mbatteryleveltextview! = NULL ){
Mbatteryleveltextview. settext (leveltext );
} Else {
View v = view. Inflate (mcontext, R. layout. battery_low, null );
Mbatteryleveltextview = (textview) v. findviewbyid (R. Id. level_percent );

Mbatteryleveltextview. settext (leveltext );

Alertdialog. Builder B = new alertdialog. Builder (mcontext );
B. setcancelable (true );
B. settitle (R. String. battery_low_title );
B. setview (v );
B. seticon (Android. R. drawable. ic_dialog_alert );
B. setpositivebutton (Android. R. String. OK, null );

Final intent = new intent (intent. action_power_usage_summary );
Intent. setflags (intent. flag_activity_new_task
| Intent. flag_activity_multiple_task
| Intent. flag_activity_exclude_from_recents
| Intent. flag_activity_no_history );
If (intent. resolveactivity (mcontext. getpackagemanager ())! = NULL ){
B. setnegativebutton (R. String. battery_low_why,
New dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int which ){
Mcontext. startactivity (intent );
If (mlowbatterydialog! = NULL ){
Mlowbatterydialog. Dismiss ();
}
}
});
}

Alertdialog d = B. Create ();
D. setondismisslistener (mlowbatterylistener );
D. getwindow (). settype (windowmanager. layoutparams. type_system_alert );
D. Show ();
Mlowbatterydialog = D;
}
// Waring voiced
Final contentresolver Cr = mcontext. getcontentresolver ();
If (settings. system. getint (Cr,
Settings. system. power_sounds_enabled, 1) = 1)
{
Final string soundpath = settings. system. getstring (Cr,
Settings. system. low_battery_sound );
If (soundpath! = NULL ){
Final URI sounduri = URI. parse ("file: //" + soundpath );
If (sounduri! = NULL ){
Final ringtone SFX = ringtonemanager. getringtone (mcontext, sounduri );
If (SFX! = NULL ){
SFX. setstreamtype (audiomanager. stream_system );
SFX. Play ();
}
}
}
}
}
// Below this line of Waring voiced is the sound of a low-power alarm.
This is probably the process!

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.