1

My use case is I want to fire my service using Job Scheduler, the moment device comes out of the Doze mode. I have tried solution provided here and in many SO question as follows, I created a BroadcastReceiver

<receiver android:name=".receiver.DozeStatusCheckReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.os.action.DEVICE_IDLE_MODE_CHANGED" />
            </intent-filter>
</receiver> 

And below is my receiver class.

package com.example.android.deviceDetail.receiver;

import android.os.PowerManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class DozeStatusCheckReceiver extends BroadcastReceiver {


    private Context mContext;
    private PowerManager mPowerManager;
    private final String TAG = DozeStatusCheckReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {


        mContext = context;

        mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        if(null != mPowerManager){

                if (mPowerManager.isDeviceIdleMode()) {
                    // the device is now in doze mode
                } else {
                    // the device is active and woke from doze mode.
                }
        }
    }
}

But it works as long as app is in foreground or in stopped state. Once app is killed from recent app list then it does not work. I have followed steps explained in doze mode testing documentation. Also I have tried registering receiver using application context.

Also I found that Android O onward background broadcast receiver does not work except for few as explained here. So is there a way to monitor state of device, whether is IDLE or Not? irrespective of Android OS version.

Shadow Droid
  • 1,696
  • 1
  • 12
  • 26
  • 2
    This is an odd requirement. Why do you need to run specifically right when the device leaves Doze? What if the device never enters Doze? Is it possible to just enqueue the job with your requirements and count on it being run once the system leaves Doze, rather than trying to detect that directly? This seems like the sort of behavior the system deliberately does not allow, because of the risk of a thundering herd of apps making the device unusable the moment it leaves Doze. – Ryan M May 12 '20 at 08:01
  • @RyanM Thank you for reply. I totally understand this is odd requirement, and you are correct with last point of risk. I have enqueued my job via job scheduler. Now my point is system does run that enqueued job but at sometimes I have noticed a longer delay. So I would like to reduce delay, how does few application work moment device comes of doze they react instantly. – Shadow Droid May 12 '20 at 08:40
  • 1
    It's possible they're just getting lucky and JobScheduler chose to run their jobs first, based on luck or heuristics. There's no "I'd really like to be first" API, because probably every app would _like_ to be first, and JobScheduler, by its nature, is for work that can be deferred until the system would like to run it (though it behaves a bit differently when your app is in the foreground). If you're trying to alert the user of, say, a time-sensitive remote event, consider high-priority FCM messages. – Ryan M May 12 '20 at 08:44

0 Answers0