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