androidandroid-7.0-nougatandroid-dozeandroid-doze-and-standby

Is it possible to track when an app enters "Doze on the Go" (AKA Doze Light, Doze Extended, and Doze 2)?


In Android "N", Doze has been extended with "Doze on the Go".

I'm looking for a way to detect when the device enters and leaves these new light doze IDLE and IDLE_MAINTENANCE states. (Basically the same question that was asked for regular Doze here.)


Solution

  • The implementation in TrevorWiley's answer works, but can be simplified a bit. Yes, Nougat's PowerManager has isLightDeviceIdleMode() and it is annotated with @hide. We can use reflection to invoke it, which is more succinct and independent of PowerManager's internal implementation details.

    public static boolean isLightDeviceIdleMode(final Context context) {
        boolean result = false;
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        if (pm != null) {
            try {
                Method isLightDeviceIdleModeMethod = pm.getClass().getDeclaredMethod("isLightDeviceIdleMode");
                result = (boolean)isLightDeviceIdleModeMethod.invoke(pm);
            } catch (IllegalAccessException | InvocationTargetException  | NoSuchMethodException e) {
                Log.e(TAG, "Reflection failed for isLightDeviceIdleMode: " + e.toString(), e);
            }
        }
        return result;
    }
    

    Agreed mostly with TrevorWiley's use of the String to register for Broadcasts. Same as the above method, you could use reflection to grab the value of field ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED and fall back to the hard-coded String "android.os.action.LIGHT_DEVICE_IDLE_MODE_CHANGED".