方式一:xml布局文件中添加属性
android:keepScreenOn="true"
方式二:Window设置flag
// 保持Activity屏幕常亮
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// 移除Activity屏幕常亮
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
方式三:使用WakeLock(不推荐)
- 添加权限
<uses-permission android:name="android.permission.WAKE_LOCK" />
- PowerManager 获取WakeLock
val powerManager = getSystemService(POWER_SERVICE) as PowerManager
val wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "WakeLock")
override fun onResume() {
super.onResume()
wakeLock?.acquire()
}
override fun onPause() {
super.onPause()
wakeLock?.release();
}
-
WakeLock相关Flag
-
PowerManager相关Flag