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.