I changed the Android SubscribeAtBoot project to reply if a message contains “doze mode test”. It helped me to discovery if emulator with Android 6.0 in Doze Mode is working.
PubnubService.java
@Override
public void successCallback(String channel, Object message) {
notifyUser(channel + " " + message.toString());
if(message.toString().toLowerCase().contains("doze mode test"))
{
pubnub.publish(channel, "Hey DM I am alive!", publishCallback);
}
}I changed the HelloWorldActivity.java to show user a REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}I tested using an emulator with SubscribeAtBoot on Android 6.0 and another emulator with DozeModeTest on Android 4.4.2.
If you want to test two projects using emulators, you need to execute commands below to put Android 6.0 emulator in Doze Mode
- Run prompt command with Administrator privileges
- Go to android-sdk\platform-tools
- adb -s emulator_name shell dumpsys deviceidle disable
- adb -s emulator_name shell dumpsys deviceidle enable
- adb -s emulator_name shell dumpsys battery unplug
- Turn the screen off
- adb -s emulator_name shell dumpsys deviceidle step => output will be Stepped to: IDLE_PENDING
- adb -s emulator_name shell dumpsys deviceidle step => output will be Stepped to: SENSING
- adb -s emulator_name shell dumpsys deviceidle step => output will be Stepped to: LOCATING
- adb -s emulator_name shell dumpsys deviceidle step => output will be Stepped to: IDLE
- adb -s emulator_name shell dumpsys deviceidle step => output will be Stepped to: IDLE_MAINTENANCE