Use Wakelock to keep your Android app awake in the background

Source: Internet
Author: User
Tags custom name

Use Wakelock to keep your Android app awake in the background-Ryan's zone-blog channel-csdn.net

In the use of some products GCA, QQ, and so on, if there is a new message, the phone screen even in the lock screen will be lit and prompt sound, then the user will know that there is a new message coming. However, in general, after the mobile phone lock screen, the Android system in order to save power and reduce CPU consumption, after a period of time will put the system into hibernation, the Android system in the CPU will remain in a relatively low power state. For the previous example, the receipt of a new message must have a network request, and the network request is CPU-consuming operation, then how to lock screen status and even after the system goes to sleep, still maintain the network state of the system and through the program to wake the phone? The answer is the Wakelock mechanism in Android.

First look at the official explanation:

PowerManager: This class gives your control of the power state of the device.

Powermanager.wakelock: Lets you say so need to has the device on.

PowerManager is responsible for the Android device power related management, and the system through a variety of locks on the power supply control, Wakelock is a lock mechanism, as long as someone holding this, the system will not enter the sleep phase. Now that you want to keep the application running in the background, it's natural to get the lock to ensure that the program always runs in the background. Before I had a requirement to run a service in the background to perform polling, but after a while, polling was interrupted (my test was 20 minutes after the request stopped), but after re-unlocking the screen, the polling request started again, and then the usage of wakelock found on StackOverflow , tried it, and it worked. Before using this method, I put the service into the front desk service and other methods are not effective, I do not know whether there is a better way in this demand, we can leave a message and I discuss!


Next look at how to use Wakelock:

[Java]View Plaincopyprint?
  1. WakeLock WakeLock = null;
  2. //Get the Power lock, keep it running when the service is still getting the CPU when the screen is off
  3. Private void Acquirewakelock ()
  4. {
  5. if (null = = WakeLock)
  6. {
  7. PowerManager pm = (powermanager)this. Getsystemservice (Context.power_service);
  8. WakeLock = Pm.newwakelock (powermanager.partial_wake_lock| Powermanager.on_after_release, "Postlocationservice");
  9. if (null ! = WakeLock)
  10. {
  11. Wakelock.acquire ();
  12. }
  13. }
  14. }
  15. //Release device Power lock
  16. Private void Releasewakelock ()
  17. {
  18. if (null ! = WakeLock)
  19. {
  20. Wakelock.release ();
  21. WakeLock = null;
  22. }
  23. }
WakeLock WakeLock = null;//Gets the power lock, keeps the service running private void Acquirewakelock () {if (null = = WakeLock) When the screen is off while still acquiring the CPU { PowerManager pm = (powermanager) this.getsystemservice (context.power_service); wakeLock = Pm.newwakelock ( powermanager.partial_wake_lock| Powermanager.on_after_release, "Postlocationservice"); if (null! = WakeLock) {wakelock.acquire ();}}} Release device power lock private void Releasewakelock () {if (null! = WakeLock) {wakelock.release (); wakeLock = null;}}

On Face the first method is to get the lock, the second method is to release the lock, once the lock is acquired, the screen after a long time off or lock screen, the system background has been able to keep getting to lock the application run. After getting the instance pm to PowerManager, the Newwakelock method gets the instance of the Wakelock, where the first parameter specifies what type of lock to get, different locks have different effects on the system CPU, screen, and keyboard, and the second parameter is a custom name.

The effects of various types of locks on the CPU, screen, and keyboard:

partial_wake_lock: Keep the CPU running, and the screen and keyboard lights may be off.

screen_dim_wake_lock: Keep the CPU running, allow the screen to be displayed but may be gray, allow to turn off the keyboard light

screen_bright_wake_lock: Keeps the CPU running, allowing the screen to be highlighted, allowing the keyboard light to be turned off

full_wake_lock: Keep the CPU running, keep the screen highlighted, and the keyboard lights remain bright

acquire_causes_wakeup: Forces the screen to illuminate, which is primarily for actions that must inform the user.

on_after_release: Keeps the screen lit for a period of time when the lock is released


Finally, don't forget to declare permissions:

& lt; android:name = "Android.permission.WAKE_LOCK " />
< android:name = "android.permission.DEVICE_ POWER "

The above on a demand for a summary of a method, a better way of friends hope to put forward in the message, discuss the improvement of the place, thank you!

Requirements: to run a service in the background to perform polling, the screen goes out or the lock screen still needs to keep the service in the polling state.


Find articles useful to you, click the Forward button in the lower right corner to share to more people!

Also welcome to follow my Sina Weibo and I exchange: @ Tang Yu _ryan

Use Wakelock to keep your Android app awake in the background

Related Article

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.