在打瞌睡模式下使用唤醒锁 setExactAndAllowWhileIdle
似乎有一些与该主题相关的问题,但我还没有找到明确的是/否的答案。
我有一个前台服务,它调用 setExactAndAllowWhileIdle 来启动 BroadcastService。下面是广播接收器中的代码。
public class StepCountUpdaterAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "myExampleApp::UpdateSteps");
wakeLock.acquire(3);
StepCounterHandler handler = StepCounterHandler.getInstance();
new TaskRunner().executeAsync(
new SetStepsForDay(SaveSharedPreference.getMemberId(context),
handler.getCumulativeSteps()),result -> {
handler.setAlarm(context);
if(isTimeToReset())
handler.resetStepsCounter();
});
wakeLock.release();
}
在 setExactAndAllowWhileIdle
文档它指出:
当警报发出时,该应用程序也会被添加到 系统临时断电列表约 10 秒 允许该应用程序获取进一步的唤醒锁 完成其工作。
但在打瞌睡模式文档中,它声明为一个限制:
系统忽略唤醒锁。
这是否意味着在 Doze 模式下 setExactAndAllowWhileIdle
调度的警报所提供的 10 秒窗口中获取 3 分钟的部分唤醒锁定实际上是无用的还是可以正常工作?
就我而言,广播接收器将通过该异步任务将数据发送到我的远程服务器,然后它将再次设置警报并重置我的步数计数器。这会起作用吗?如果不起作用,在打瞌睡模式下发送网络请求并执行后续代码的替代方案是什么?
编辑:通过调试我的应用程序进行测试表明,当强制设备进入空闲模式时,我仍然具有网络访问权限并且可以将数据发送到我的服务器。打瞌睡模式文档说明了如何强制应用程序进入空闲状态,我相当确定这与打瞌睡模式同义。但这应该是一个限制,所以我不知道这是如何运作的。
It seems there are a few questions related to the subject topic but I haven't found a clear yes/no answer to this.
I have a foreground service that calls setExactAndAllowWhileIdle
to start a BroadcastService. Below is the code in the Broadcast Receiver.
public class StepCountUpdaterAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "myExampleApp::UpdateSteps");
wakeLock.acquire(3);
StepCounterHandler handler = StepCounterHandler.getInstance();
new TaskRunner().executeAsync(
new SetStepsForDay(SaveSharedPreference.getMemberId(context),
handler.getCumulativeSteps()),result -> {
handler.setAlarm(context);
if(isTimeToReset())
handler.resetStepsCounter();
});
wakeLock.release();
}
In the setExactAndAllowWhileIdle
documentation it states:
When the alarm is dispatched, the app will also be added to the
system's temporary power exemption list for approximately 10 seconds
to allow that application to acquire further wake locks in which to
complete its work.
but in the Doze mode documentation it states this as a restriction:
The system ignores wake locks.
Does that mean acquiring a partial wake lock for 3 minutes in the 10 second window provided by an alarm dispatched by setExactAndAllowWhileIdle
in Doze mode will effectively be useless or will it work fine?
In my case, the Broadcast Receiver will send data to my remote server via that async task, and after that it will set the alarm again and reset my steps counter. Will this work and if it wont, what are my alternatives for sending a network request in doze mode and executing follow up code?
EDIT: Testing by debugging my app shows that when forcing a device into idle mode, i still have network access and can send data to my server. The Doze mode documentation states how to force to app into the idle state which i am fairly sure is synonymous with doze mode. Yet this is supposed to be a restriction so I am clueless as to how this could be working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果设备处于打瞌睡模式,
WakeLock
对您没有多大帮助。如果您使用AlarmManager.ELAPSED_REALTIME_WAKEUP
或AlarmManager.RTC_WAKEUP
作为闹钟类型
,setExactAndAllowWhileIdle
将唤醒您的设备。但是,从 Android 12 开始,您需要
SCHEDULE_EXACT_ALARM
权限来设置确切的警报。如果您计划将应用程序发布到 PlayStore,则有一些 设置精确警报的可接受用例。确保您的应用程序符合这些政策。A
WakeLock
won't help you much if the device is in Doze mode.setExactAndAllowWhileIdle
will wake your device up if you have used eitherAlarmManager.ELAPSED_REALTIME_WAKEUP
orAlarmManager.RTC_WAKEUP
as the alarmtype
.However, from Android 12 you need
SCHEDULE_EXACT_ALARM
permission to set exact alarms. And if you are planning to release the app to PlayStore there are some acceptable use cases for setting an exact alarm. Make sure your app complies with these policies.是的,Doze 会忽略您的唤醒锁。然而,使用 setExactAndAllowWhile Idle,您将在正确的时间开始工作,并且您将有 10 秒的窗口来执行您希望的任何处理。
Yes, Doze will ignore your wakelock. However with setExactAndAllowWhile Idle you will be worken up at the correct time, and you'll have that 10s window to do any processing you wish.