One of the app's power optimization tasks scheduled according to battery conditions

Source: Internet
Author: User

Here to talk about the task scheduling according to the current characteristics of the battery. For a power-hungry task, it needs to be done with a more abundant battery, and if the power is already withered, doing so will drain all the power.
Based on this, before doing such a task, obtain the current battery characteristics and make use of the battery characteristics to determine whether to perform the task.
Detailed explanations are as follows:
1 getting the current battery characteristics

/*gets the current battery status. Setting Broadcastreceiver to NULL will return the status of the current battery directly. */Intentfilter IFilter=NewIntentfilter (intent.action_battery_changed); Intent Batterystatus= Registerreceiver (NULL, IFilter); /*** Battery charge status * public static final int battery_status_unknown = 1; Unknown state * public static final int battery_status_charging = 2; Charging in * public static final int battery_status_discharging = 3; Discharge in * public static final int battery_status_not_charging = 4; Not pure Power * public static final int battery_status_full = 5; State of full power*/        intStatus = Batterystatus.getintextra (Batterymanager.extra_status,-1); /*** Battery Charging mode, if the phone is charging the situation, you can do some power consumption, where AC charging can be used to the maximum. Second USB charging * public static final int battery_plugged_usb = 2; USB Charging * public static final int battery_plugged_wireless = 4; Wireless Charging * public static final int battery_plugged_ac = 1; AC charging, alternating current charging*/        intChargeplug = Batterystatus.getintextra (batterymanager.extra_plugged, 1); /*** Battery charge level*/        intLevel = Batterystatus.getintextra (Batterymanager.extra_level,-1); intScale = Batterystatus.getintextra (Batterymanager.extra_scale,-1); floatbatterypct = Level/(float) scale; /*** public static final int battery_health_unknown = 1;    Health status Unknown * public static final int battery_health_good = 2;  Health * public static final int battery_health_overheat = 3;    Overheat * public static final int battery_health_dead = 4;  No electricity * public static final int battery_health_over_voltage = 5; OVER voltage * public static final int battery_health_unspecified_failure = 6; Undefined error * public static final int battery_health_cold = 7; Cooling*/        intHealth = Batterystatus.getintextra (Batterymanager.extra_health,-1);

As for the task that should be performed at that time, I think the reader will be able to tell the difference as long as the task is not battery_health_dead.

2 Monitor battery charge status. Battery charging is often a good time to work, when you do, electricity can meet
Register for monitoring

<receiver android:name= ". Battertyconnectreceiver ">        <intent-filter>            <!--phone link charging or disconnection caused by change--            <action android: Name= "Android.intent.action.ACTION_POWER_CONNECTED"/>             <action android:name= " Android.intent.action.ACTION_POWER_DISCONNECTED "/>        </intent-filter>    </receiver>

Receive broadcasts

 Public classBattertyconnectreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {intStatus = Intent.getintextra (Batterymanager.extra_status,-1); if(Status = =batterymanager.battery_status_charging) {            //charging status, good timing}Else if(Status = =batterymanager.battery_status_not_charging) {            //Disconnect Charging Status            }        }}

3 Monitor Battery low power change status
Register for monitoring

<receiver android:name= ". Batterylevelreceiver ">        <intent-filter>            <!--when the power entry is low or when the power is not enough to enter the foot.-            <action Android:name= "Android.intent.action.ACTION_BATTERY_LOW"/>            <action android:name= " Android.intent.action.ACTION_BATTERY_OKAY "/>        </intent-filter>    </receiver>

Receive broadcasts

 Public classBatterylevelreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); if(Action_battery_low.equals (ACTION)) {//the power goes to the stall, do the task state save}Else if(Action_battery_okay.equals (ACTION)) {//The power goes to sufficient gear            }        }}

The above API is a basic judgment on battery characteristics. Need to do the right thing in the right battery state

One of the app's power optimization tasks scheduled according to battery conditions

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.