android保持屏幕常亮方式

方式一: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(不推荐)
  • 添加权限

  • 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


    WakeLock-Flags.png
  • PowerManager相关Flag


    PowerManager-Flags.png

你可能感兴趣的:(android保持屏幕常亮方式)