控制屏幕常亮不熄灭
方式1
- 代码里调用
val pm = context!!.getSystemService(Context.POWER_SERVICE) as PowerManager
val wakeLock = pm.newWakeLock(
PowerManager.FULL_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP,
""
)
wakeLock.acquire()
- 清单文件需要配置权限
<uses-permission android:name="android.permission.WAKE_LOCK"/>
- 取消控制屏幕常亮不熄灭得控制
swakeLock.release()
方式2
利用View提供的方法
/**
* Controls whether the screen should remain on, modifying the
* value of {@link #KEEP_SCREEN_ON}.
*
* @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
*
* @see #getKeepScreenOn()
*
* @attr ref android.R.styleable#View_keepScreenOn
*/
public void setKeepScreenOn(boolean keepScreenOn) {
setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
}
- 屏幕常亮
button.keepScreenOn = true
- 取消常亮
button.keepScreenOn = false