skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. A WakeLock won’t help you much if the device is in Doze mode. setExactAndAllowWhileIdle will wake your device up if you have used either AlarmManager.ELAPSED_REALTIME_WAKEUP or AlarmManager.RTC_WAKEUP as the alarm type.

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search