Example usage for android.os PowerManager isDeviceIdleMode

List of usage examples for android.os PowerManager isDeviceIdleMode

Introduction

In this page you can find the example usage for android.os PowerManager isDeviceIdleMode.

Prototype

public boolean isDeviceIdleMode() 

Source Link

Document

Returns true if the device is currently in idle mode.

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.M)
public static boolean isDeviceIdleMode(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return false;
    }//from ww w. j  av a 2  s  .co  m

    PowerManager pm = (PowerManager) context.getSystemService(Service.POWER_SERVICE);
    return pm.isDeviceIdleMode();
}

From source file:com.mendhak.gpslogger.common.Systems.java

/**
 * Returns true if the device is in Doze/Idle mode. Should be called before checking the network connection because
 * the ConnectionManager may report the device is connected when it isn't during Idle mode.
 * https://github.com/yigit/android-priority-jobqueue/blob/master/jobqueue/src/main/java/com/path/android/jobqueue/network/NetworkUtilImpl.java#L60
 *///from   www . j  a v  a2 s .  c  o m
@TargetApi(23)
public static boolean isDozing(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return powerManager.isDeviceIdleMode()
                && !powerManager.isIgnoringBatteryOptimizations(context.getPackageName());
    } else {
        return false;
    }
}