Android * wake-up lock

Source: Internet
Author: User

WakeLock_WhitePaper.doc
BSD2.0.txt
WakeLock.zip

Christopher Bird

Android power management-wake up lock New Market

Most people may experience the embarrassment of a short battery period on their mobile phones. This is annoying. There is no difference between a cell phone with no power and a cement brick. In general, if a user's cell phone cannot last for a whole day, they will feel very dissatisfied. In addition, when the mobile phone is charged, the user cannot use the mobile phone, which also brings great inconvenience.

Traditionally, jobs that require laptop or PC processing can now be processed using smartphones even if they are not at the desk with all the new improved software. However, compared with a laptop, the small shape of the smartphone greatly limits the size of the battery that can be accommodated. It is difficult to provide a mobile phone with a laptop and a 24-hour battery life.

With excellent power management, Android and other mobile operating systems provide a durable battery life. Shortly after the phone is stopped, the monitor will be shut down, and the CPU will enter the deep energy-saving state. Therefore, when you do not use it, it will only consume a small amount of power. This is why the phone can be used for multiple days after it is fully charged. With the power manager of Android, the CPU is also disabled when the monitor is disabled.

However, Android Developers can (and have permissions) Prevent Android devices from entering sleep mode. They may want the CPU to be active-even if the monitor is off. Or they may want to prevent the monitor from automatically disabling when performing an activity. For this reason, Google * adds a wake-up lock in its PowerManager API. Applications that prevent devices from entering sleep mode can use wake-up locks. As long as there is an active wake-up lock on the system, the device cannot enter the suspend mode unless the wake-up lock is released. When using the wake-up lock, you must understand that when you do not need to wake up the lock, you must release it correctly, because the unreleased wake-up lock cannot enter the default status to save energy, this will soon exhaust the device's battery.

This article will introduce you to some Android apps (and usage modes) that use the wake-up lock by default in Android 4.0 to help you understand when to use this technology. Next, we will introduce a sample application "Wakelocks" in the SDPSamples set to show how to write the code for wakeup locks.

Waken Application Usage

With Android, there is a way to check which services enable the wake-up lock to prevent the system from entering the low power consumption status. The/proc/wakelocks file on the device lists the services and drivers that define the use of wake-up locks. By monitoring the content of the/sys/power/wake_lock file (root access is required), you can know when the wake-up lock is enabled for CPU resources and which service has wakelock2 enabled. I have captured several use cases where the wake-up lock is enabled on Galaxy Nexus running Android 4.0, as shown below:

Application used Executed operation Services with wake-up locks Running status
Arbitrary Press the UI Widget (for example, click a button or ListView) PowerManagerService Enabled and locked after 5 seconds
MAP/navigation Enable map or enter navigation Gps-lock Enable locking and use GPS
YouTube Watch Streaming Videos PowerManagerService Enable the wake-up lock throughout video playback.
Music Listen to music PowerManagerService Enable the wake-up lock during music playing

Table: some default Android applications demonstrate the use of wake-up locks

The YouTube and Music apps can demonstrate different levels of wake-up locks. When you play a video, the YouTube app will enable the wake-up lock. During the video playing process, the display will remain on (ignore the system display settings ). However, if you press the power button during playback, the device will be suspended, which will cause the display to shut down and the audio/video to stop playing. The Music application uses different wake-up locks when playing audio. The display settings cannot be changed, so the screen of the device will be closed based on the user's display settings. When the monitor is off, the wake-up lock keeps the CPU active so that the audio can continue playing-even if the user presses the power button.

Select wake-up lock (before execution)

Before learning how to write a wake-up lock, you must understand the types of the wake-up lock to select the most appropriate wake-up lock for your application. The Android PowerManager API introduces multiple wake-up lock tags used to change the power status of a device:

Tag Value CPU Screen Keyboard
PARTIAL_WAKE_LOCK Enable Close Close
SCREEN_DIM_WAKE_LOCK Enable Dim) Close
SCREEN_BRIGHT_WAKE_LOCK Enable Bright (Bright) Close
FULL_WAKE_LOCK Enable Bright (Bright) Bright (Bright)

Table: Derived from the Android PowerManager API.

This API highlights that wake-up locks can significantly shorten the battery life of Android devices. Therefore, you should avoid using them as much as possible. If used, release it as soon as possible.

The application that uses the wake-up lock must apply for a special license before it can be executed. This can be achieved through the android. permission. WAKE_LOCK license in the Application List file. That is to say, when a user installs an application that uses the wake-up lock through Google Play, the system will remind them that the features contained in the application may be "Prevent phone from sleeping (preventing the phone from going to sleep )". If developers want to prevent the display of an application from being dimmed under certain conditions, they can use another method provided by Google, which requires no special permission. WindowManager includes a FLAG_KEEP_SCREEN_ON variable. You can set the FLAG_KEEP_SCREEN_ON variable when the application view is disabled. We recommend that you use this method in display control because its impact is independent of each other in applications. After the user task is switched to another application, WindowManager immediately releases the wake-up lock.

Keep the monitor on (derived from SDPSamples)

The wake-up lock application of the SDPSamples set demonstrates (including code) how an application uses Window Manager instead of enabling the display by writing the wake-up lock code. After the wake-up lock application is enabled, select the "Win Man Screen On" list item.

As long as the button status shows "Screen is LOCKED", the interface will remain in the enabled status. After the button status is changed to "Screen is UNLOCKED", if it is not operated for more than 5 seconds, the display will be closed.

In the Code, each time you press the button and the status changes, you can use the screenLockUpdateState () function in WakeLockActivity. java to set and release the FLAG_KEEP_SCREEN_ON variable of the current window.

 
 
01 public void screenLockUpdateState() {
02  
03     if (mIsDisplayLocked)
04      {
05         ...
06         // update display state
07       getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
08     } else
09 {
10         ...
11         // update display state
12           getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
13     }
14 }
Write a wake-up lock (from SDPSamples)

The wake-up lock application in the SDPSamples set also includes the code for executing different wake-up locks. After the Wake-up Lock application is started, choose from the following four types of Wake-up locks: Power Wake Lock Full, Power Wake Lock Bright, Power Wake Lock Dim, and Power Wake Lock Partial. The four list items correspond to the four wake-up lock tags described in the PowerManager API. Each item demonstrates how the device responds when it tries to shut down the screen within five seconds.

By monitoring the content of the/sys/power/wake_lock file (root access is required), you can see that after the power button is pressed, PARTIAL_WAKE_LOCK is the only and still valid wake-up lock. Other wake-up locks prevent the display from being closed with multiple levels of brightness.

The first step in writing a wake-up lock is to apply for a license before you can use the wake-up lock in the list AndroidManifest. xml:

After completing this step, you can create a wake-up Lock Object, including the functions for controlling the wake-up lock acquisition () and release. The encoding in the WakeLockActivity. java file shows the situation well:

 
 
01 public void onCreate(Bundle savedInstanceState) {
02     ...
03     mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
04     ...
05     mWakeLock = mPowerManager.newWakeLock(mWakeLockState,
06                         "UMSE PowerTest");
07     if (mWakeLock != null) {
08         mWakeLock.acquire();
09     ...
10     }
11 }
12  
13 protected void onDestroy() {
14     if (mWakeLock != null) {
15         mWakeLock.release();
16         mWakeLock = null;
17     }
18     ...
19 }
Conclusion

Wake-up lock is a powerful concept in Android. It allows developers to modify the default Power status of a device. The risk of using a wake-up lock in an application: it reduces the battery life of the device. The obvious advantages of the wake-up lock can be seen in a variety of default applications provided by Google, such as road navigation and music/video playback. Therefore, developers should determine whether their application models can benefit from the use of wake-up locks.

About the author

Christopher Bird entered Intel's SSG in 2007 and is committed to building an ecosystem that supports lingdong devices (mobile phones and tablets ).

Reference

1 Android reference: http://developer.android.com/reference/android/ OS /PowerManager.html

2 LWN-"wakeup lock and embedded problems": http://lwn.net/Articles/318611/

Statement

This document contains information about Intel products. This document does not constitute authorization for any intellectual property rights, including express, implied, or based on the Anti-statement principle or others. Intel is not liable for any other responsibilities. Intel hereby disclaims: this document does not constitute any express or implied warranty of Intel regarding the use and/or sale of its products, including) any liability or warranty for the applicability of a specific purpose, the (ii) merchantability and (iii) Infringement of any patent, copyright or other intellectual property rights.

Intel products are intended for or used for the following applications without the prior written consent of Intel: In such applications, personal injury or death may be caused by faults of Intel products.

Intel has the right to change product specifications and descriptions at any time without notice. The designer shall not trust any feature that is not available to the English product, nor shall the designer trust any description or feature description marked with "reserve right" or "undefined. In this regard, Intel reserves the right to define it in the future. At the same time, Intel shall not assume any responsibility for conflicts and incompatibility arising from its subsequent changes to such descriptions or feature descriptions. The information in this article may be changed at any time without notice. Do not use this document to complete a product design.

The products described in this document may contain design defects or errors that make them inconsistent with the declared specifications. These defects or errors are included in the error table and can be obtained upon request. Before placing an order, contact your local intel business department or distributor to obtain the latest product specifications.

To obtain a copy of the document containing the order number in this document or other intel materials, call 1-800-548-4725 or log on to the http://www.intel.com/design/literature.htm

The software and workload used in performance testing may be optimized only on Intel microprocessor for performance. Tests such as SYSmark and MobileMark are based on specific computer systems, hardware, software, operating systems and functions. Changes to any of the above elements may lead to changes in the test results. Refer to other information and performance tests (including running performance when used in conjunction with other products) to fully evaluate the target product.

The provision of software source code contained in this document is based on the relevant software license and any use and reproduction of such source code shall be subject to the terms of the relevant software license.

The Intel and Intel logos are trademarks of Intel in the United States and/or other countries.

Intel reserves all rights reserved on April 9, 2012. All rights are protected.

* Other names and brands may be assets of other owners.

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.