AlarmManager 和 PowerManager.WakeLock


这是一篇关于alarmManager和PowerManager的解释性文章。初衷在于碰到了android在不连接电源的情况下锁屏一段时间后,cpu会进入休眠状态,会导致定位失败,收不到消息等问题,需要做到唤醒cpu来继续自己的操作。

最终结论是:保持屏幕唤醒是最佳方式。 定时闹钟,监听系统广播,唤醒CPU,都不能正常取到定位信息。当然,能取到系统签名,是另外一回事。

AlarmManager

> This class provides access to the system alarm services.  These allow you to schedule your application to be run at some point in the future.  When an alarm goes off, the intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running.

是系统级别的,和app是否在运行无关。定时由系统来发广播,运行broadcastReceiver里面的内容。

> The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast. Once onReceive() returns, the Alarm Manager releases this wake lock. This means that the phone will in some cases sleep as soon as your onReceive() method completes.  If your alarm receiver calledContext.startService(), it is possible that the phone will sleep before the requested service is launched. To prevent this, your BroadcastReceiver and Service will need to implement a separate wake lock policy to ensure that the phone continues running until the service becomes available.

alarmManger持有cpu wakeLock 当receiver里面的代码在执行时。receiver里面的代码执行完,就释放这个WakeLock。

另开线程或者启动服务,可能会在wakeLock release之后,就执行不了。

最好需要自己来持有一个WakeLock来保证需要的代码执行完成。

Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.  For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to useHandler.

如果仅仅需要在app运行时执行计时等工作,handler更适合,效率也更高。

Note:Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use.  There are new APIs to support applications which need strict delivery guarantees; seesetWindow(int, long, long, PendingIntent)andsetExact(int, long, PendingIntent).  Applications whosetargetSdkVersionis earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.

如果app的TargetSdk 大于19,系统会处于省电考虑,推迟Alarms。

PowerManager.WakeLock

A wake lock is a mechanism to indicate that your application needs to have the device stay on.

wakelock 用来表示你的app需要设备保持唤醒状态。

Any application using a WakeLock must request  the android.permission.WAKE_LOCK permission  in anelement of the application's manifest. Obtain a wake lock by calling new WakeLock(int, String).

需要申请权限 WAKE_LOCK.可以直接new出来对象。

Call acquire() to acquire the wake lock and force the device to stay on at the level that was requested when the wake lock was created.

acquire()方法表示,当wakeLock创建的时候,强迫设备保持请求的level。(int型类型)

没搞懂,不知道是否在release前面都保持这个level。

Call release() when you are done and don't need the lock anymore. It is very important to do this as soon as possible to avoid running down the device's battery excessively.

调用release()释放资源,可以有效的省电。

官方demo

PowerManagerpm=(PowerManager)getSystemService(Context.POWER_SERVICE);

PowerManager.WakeLockwl=pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,"My Tag");

wl.acquire();

..screen will stay on duringthissection..

wl.release();

AlarmManager 和 PowerManager.WakeLock_第1张图片
new WakeLock(int,string)中的int类型tag

*

If you hold a partial wake lock, the CPU will continue to run, regardless of any

display timeouts or the state of the screen and even after the user presses the power button.

选择PARTIAL_WAKE_LOCK的时候,cpu会保持运转,就算设置的TimeOut或者按下锁屏键都一样。

In all other wake locks, the CPU will run, but the user can still put the device to sleep

using the power button.

其他的tag里面,锁屏键按下之后,会导致设备进入休眠模式。


你可能感兴趣的:(AlarmManager 和 PowerManager.WakeLock)