How Android keeps the service from sleeping (running continuously) and waking up the screen

Source: Internet
Author: User

Suppose there is a scenario where a service has been working silently (such as getting the server's messages on the fly), even if the screen has been turned off for a long time, or if the user presses the power key to turn the screen off and the phone goes into hibernation, he must still be working. Once the message is retrieved from the server, he can light up the screen and do some processing work.

1, let the service continue to operate without being affected by the sleep of the phone, to deal with this:

Add in OnCreate:

PM = (PowerManager) Myapplicationholder.getcontext (). Getsystemservice (Context.power_service);   " MyService " );  Wl.acquire ();  

Add in OnDestroy:

Wl.release ();  

This is done, and if a timer is made in the service, he will keep running.

2. How to wake the phone from hibernation:
Keyguardmanager km = (Keyguardmanager) Myapplicationholder.getcontext (). Getsystemservice (Context.KEYGUARD_ SERVICE);   = Km.newkeyguardlock ("unlock");  Kl.disablekeyguard ();     = (PowerManager) Myapplicationholder.getcontext (). Getsystemservice (Context.power_service);   " Bright " );  Wl.acquire ();  Wl.release ();  

Note that the screen cannot be locked using this method. Android.permission.DISABLE_KEYGUARD Permissions Required

How Android keeps the service from sleeping (running continuously) and waking up the screen

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.