简体   繁体   English

setExactAndAllowWhileIdle 在打瞌睡模式下使用唤醒锁

[英]setExactAndAllowWhileIdle with Wakelock in Doze mode

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.我有一个调用setExactAndAllowWhileIdle来启动 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:setExactAndAllowWhileIdle 文档中指出:

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.发出警报后,该应用程序还将被添加到系统的临时电源豁免列表中大约 10 秒,以允许该应用程序获取更多唤醒锁以完成其工作。

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?这是否意味着在 10 秒 window 中通过setExactAndAllowWhileIdle在 Doze 模式下发出的警报提供的 3 分钟部分唤醒锁定实际上是无用的,或者它会正常工作吗?

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.打瞌睡模式文档说明了如何强制应用程序进入空闲状态 state,我相当确定这是打瞌睡模式的同义词。 Yet this is supposed to be a restriction so I am clueless as to how this could be working.然而,这应该是一个限制,所以我不知道它是如何工作的。

A WakeLock won't help you much if the device is in Doze mode.如果设备处于 Doze 模式, WakeLock将无济于事。 setExactAndAllowWhileIdle will wake your device up if you have used either AlarmManager.ELAPSED_REALTIME_WAKEUP or AlarmManager.RTC_WAKEUP as the alarm type .如果您使用AlarmManager.ELAPSED_REALTIME_WAKEUPAlarmManager.RTC_WAKEUP作为闹钟typesetExactAndAllowWhileIdle将唤醒您的设备。

However, from Android 12 you need SCHEDULE_EXACT_ALARM permission to set exact alarms.但是,从 Android 12 您需要SCHEDULE_EXACT_ALARM权限才能设置准确的警报。 And if you are planning to release the app to PlayStore there are some acceptable use cases for setting an exact alarm .如果您打算将应用程序发布到 PlayStore,则有一些可以接受的用例来设置准确的警报 Make sure your app complies with these policies.确保您的应用符合这些政策。

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.但是,使用 setExactAndAllowWhile Idle,您将在正确的时间开始工作,并且您将拥有 10s window 来执行您希望的任何处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM