Android设置系统休眠

可以通过以下方法进行系统休眠时间的获取,设置休眠时间,唤醒屏幕等:(测试环境Android5.1)

/**
 * 设置休眠时间
 *
 * @param millisecond
 * @param context
 */
public static void setScreenSleepTime(int millisecond, Context context) {
    try {
        Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT,
                millisecond);
    } catch (Exception localException) {
        localException.printStackTrace();
    }
}

/**
 * @param context
 * @return
 */
public static int getScreenSleepTime(Context context) {
    int millisecond = 0;
    try {
        millisecond = Settings.System.getInt(context.getContentResolver(),
                Settings.System.SCREEN_OFF_TIMEOUT);
    } catch (Exception localException) {

    }
    return millisecond;
}


/**
 * 唤醒手机屏幕并解锁
 */
public static void wakeUpAndUnlock() {
    BLog.e("唤醒手机屏幕并解锁");
    PowerManager pm = (PowerManager) GlobalApplication.getInstance().getApplicationContext()
            .getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(
            PowerManager.ACQUIRE_CAUSES_WAKEUP |
                    PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "wakup");
    wl.acquire(10000); 
    wl.release(); 

    KeyguardManager keyguardManager = (KeyguardManager) GlobalApplication.getInstance().getApplicationContext()
            .getSystemService(KEYGUARD_SERVICE);
    KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("unLock");

    keyguardLock.reenableKeyguard();
    keyguardLock.disableKeyguard();
}

/**
 * 判断当前屏幕是否亮
 *
 * @return
 */
public static boolean isScreenOn() {
    PowerManager pm = (PowerManager) GlobalApplication.getInstance().getApplicationContext()
            .getSystemService(Context.POWER_SERVICE);
    boolean screenOn = pm.isScreenOn();

    return screenOn;
}




需要添加的权限:

android:name="android.permission.WRITE_SETTINGS"/>
android:name="android.permission.WAKE_LOCK" />
android:name="android.permission.DISABLE_KEYGUARD" />





你可能感兴趣的:(Android)