Effects of hibernation on the program and processing of Android system

Source: Internet
Author: User

When the Android system is not operating for a long time, the system chooses to hibernate in order to conserve resources. The timer, Handler, Thread, service, and so on, which are customized during hibernation, will be paused. And sometimes this mechanism can affect the normal operation of our program. So how to avoid the effect of system hibernation on our program?

The following service services are examples of how to wake up the system and perform our custom service services in a system hibernation state.

First, the opening of a single service

1. Method: Obtain the Power lock when the service is required, and release the Power lock after the execution is complete.

2, code example:

public class wakelockutil{public static WakeLock WakeLock = null;  public static void Acquirewakelock (Activity myactivity) {if (null = = WakeLock) {PowerManager            PM = (PowerManager) myactivity.getsystemservice (Context.power_service); WakeLock = Pm.newwakelock (Powermanager.on_after_release |            Powermanager.partial_wake_lock, "Wakelockutil"); Partial_wake_lock: Keep the CPU running, the screen and keyboard lights may be off-most commonly, keep the CPU running//Screen_dim_wake_lock: Keep the CPU running, allow the screen to be displayed but may be gray, allow off Closed keyboard light///Screen_bright_wake_lock: Keep the CPU running, allow the screen to be highlighted, allow to turn off the keyboard Light//Full_wake_lock: Keep the CPU running, keep the screen highlighted, and the keyboard light            Maintain brightness//acquire_causes_wakeup: Forces the screen to illuminate, which is primarily for actions that must inform the user. On_after_release: When the lock is released, keep the screen lit for a period of time if (null! = WakeLock) {wakelock.acquire (); Get power Lock now//Wakelock.acquire (2000);     2 seconds to get power Lock}}} public static void Releasewakelock (Activity myactivity) {   if (null! = WakeLock) {wakelock.release ();        WakeLock = null; }    }}

3. How to explain

Acquirewakelock (): Wakes up the system to obtain a power lock.

Releasewakelock (): To release the Power Lock (to release the Power lock in time after the completion of the execution, otherwise it will be compared with electricity)

Second, timed duplicate service open

1, using the Android to bring the timer Alarmmanager implementation

Intent Intent = new Intent (Mcontext, Servicetest.class); pendingintent pi = Pendingintent.getservice (mcontext, 1, intent, 0); Alarmmanager alarm = (Alarmmanager) getsystemservice (Service.alarm_service); if (alarm! = null) {    alarm.cancel (pi);    The alarm clock wakes the system and performs the prompt function in the system sleep    alarm.setrepeating (Alarmmanager.rtc_wakeup, System.currenttimemillis () + 1000, 2000, PI);    Alarm.set (Alarmmanager.rtc_wakeup, System.currenttimemillis (), pi);}

2, the timer can start service services, send broadcasts, jump activity, and will wake up the system in the sleep state. Therefore, this method does not need to acquire the power lock and release the Power lock.

  

Effects of hibernation on the program and processing of Android system

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.