当前位置:   article > 正文

Android设置系统休眠_android休眠调整许可证

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;
}




需要添加的权限:

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





声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/301733
推荐阅读
相关标签
  

闽ICP备14008679号