App power consumption Optimization II use the source management to schedule tasks

Source: Internet
Author: User

PowerManager Power Management (Power usage management). Main management devices start, keep active, Hibernate, wake up. The Powermanager.wakelock (wake-up lock) is provided in order to maintain the task. Holding this wake-up lock during a task can prevent hibernation and thus perform the task normally. Of course, the cost is to increase the power consumption. In another case, if the lock is not released after the task is executed, the device does not go into hibernation, thereby increasing the power consumption. In fact, Wakelock is standing in the opposite of power optimization, but there are a lot of scenes do not use, such as listening to music, watching the process of video to want the device to hibernate it?

So in many scenarios it is necessary to consider using Wakelock, and many scenarios have no need to use Wakelock. For example, the use of e-books to read, you need to wakelock prevent the screen, QQ, using Wakelock keep the background running, you can receive messages.

Use of WakeLock:

1 Adding usage rights

<uses-permission android:name= "Android.permission.WAKE_LOCK"/><uses-permission android:name= " Android.permission.DEVICE_POWER "/>

2 Specific Use

//Create Wakelock, lock type is Powermanager.partial_wake_lockPowerManager PowerManager =(PowerManager) Getsystemservice (Power_service); Powermanager.wakelock Mwakelock= Powermanager.newwakelock (Powermanager.partial_wake_lock, "Mywakelock"); //Get Wakelock    if(Mwakelock! =NULL) {Mwakelock.acquire ();} //********** performing Tasks *************//   //release Wakelock, allow the device to enter hibernation if(Mwakelock! =NULL&& Mwakelock.isheld ()) {mwakelock.release ();}

The above is the simplest way to use. The most important thing is to have a mwakelock.release (); Be sure to release the lock. Otherwise the power consumption is violent.

Based on the above, the optimization points needing attention are as follows:
1 using the appropriate lock type

Powermanager.partial_wake_lock only ensures that the CPU is running, and that the keyboard lights are not guaranteed as the screen is often lit. Commonly used wake-up lock powermanager.screen_dim_wake_lock Ensure that the CPU is running, and that the screen remains gray and the keyboard light is not guaranteed. The Powermanager.screen_bright_wake_lock ensures that the CPU, the screen is always lit, and the brightness is the highest, the keyboard light does not keep. The Powermanager.full_wake_lock ensures that the  CPU is running, that the screen is always on, and that the keyboard light is always lit. The powermanager.acquire_causes_wakeup ensures that the CPU is running and the screen is lit, even if the original screen is off, and the keyboard light is not maintained.          powermanager.screen_dim_wake_lock Force the screen and keyboard light to open. Powermanager.on_after_release after the lock is released, the screen status will persist for a period of a year. Powermanager.wait_for_proximity_negative with distance sensors, when the hand is close to the screen when the phone is on, and the screen is left

2 appropriate handling lock-in time
Sometimes it is not possible to determine how long a task will take to be held during a lock. For example, access to the network is likely to time out, causing the program to fail to execute further and cannot release the lock further. Based on this in the hold lock is required to make a timeout limit, which exceeds the timed natural release lock.

if null) {Mwakelock.acquire (10000); // hold lock for 1 seconds after release}

3 the shorter the lock time, the better.

In addition, the PowerManager provides the following interfaces

1 gotosleep () forced into hibernation
2 Wakeup () forced from hibernation into wake
3 useractivity () reports to powermanagerservice the user activity that affects the system hibernation, recalculates the splash time, backlight brightness, etc., such as touch screen, screen, power key and other user activities.
The above three methods are not open to the upper apk, and need two permissions

<uses-permission android:name= "Android.permission.REBOOT"/><uses-permission android:name= " Android.permission.DEVICE_POWER "/>

4 reboot (String reason), restart, non-app interface. Add Permission Android.permission.REBOOT.

Others remain on the screen, these are just adjustments to the power of the screen, valid for the current window

// when the lock screen is still visible, the window can be displayed.                 // when window is visible to the user, it is highlighted                // when activity is used to light the screen    

Or

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android        android:layout_width=" Wrap_ Content "        android:layout_height=" Wrap_content "        android:keepscreenon=" true ">

App power consumption Optimization II use the source management to schedule tasks

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.