直接从AlarmManager唤醒服务直接、AlarmManager

2023-09-06 03:24:43 作者:Amnesia 记忆缺失

在 https://github.com/commonsguy/cwac-wakeful 的演示中,OnAlarmReceiver(一个BroadcastReceiver)的onReceive()方法被调用响应于报警。该的onReceive()方法启动服务。有用于两个目的,一是由接收的BroadcastReceiver和一个由服务。这似乎更复杂,它需要的,为什么不只是有服务接收来自AlamManager的意图是什么?

据我了解,这款手机是保证不会进入休眠状态的onReceive()被执行,即它包裹着一个唤醒锁。我不知道,如果服务类提供任何类似的保证。

是没有办法直接从报警启动的服务,同时还保证,该电话将被从睡梦中唤醒,也不会睡觉,直到唤醒锁可以获取?

解决方案   B端产品经理的能力模型与学习提升

这似乎更复杂,它需要的,为什么不只是有服务接收意向从AlamManager?

由于Android将不能保证设备将保持清醒足够长的时间的getService() PendingIntent 是调用。该担保的只有的的 getBroadcast() PendingIntent - 安卓确保设备将保持清醒(通过 WakeLock )的期间你的的onReceive()电话。

相信我,我希望我们可以跳过它。

In the https://github.com/commonsguy/cwac-wakeful demo, the OnAlarmReceiver (a BroadcastReceiver) onReceive() method is called in response to an Alarm. The onReceive() method starts the Service. There are two Intents used, one received by the BroadcastReceiver and one by the Service. This seems more complicated that it needs to be, why not just have the Service receive the Intent from the AlamManager?

I understand that the Phone is guaranteed not to sleep while onReceive() is executing i.e. it wrapped with a wake lock. I'm not sure if the Service class offers any similar guarantees.

Is the any way to start the Service directly from an Alarm while still guaranteeing that the Phone will be woken from sleep and won't sleep until a wake lock can be acquired?

解决方案

This seems more complicated that it needs to be, why not just have the Service receive the Intent from the AlamManager?

Because Android will not guarantee that the device will stay awake long enough for a getService() PendingIntent to be invoked. The guarantee is only for a getBroadcast() PendingIntent -- Android ensures that the device will stay awake (via a WakeLock) for the duration of your onReceive() call.

Trust me, I wish we could skip it.

 
精彩推荐
图片推荐