Android保持屏幕常亮2种实现方法

时间:2022-12-04 22:06:09

方法一:

复制代码 代码如下:

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

 

复制代码 代码如下:

private PowerManager powerManager = null;
private WakeLock wakeLock = null;
 
powerManager = (PowerManager) this.getSystemService(Service.POWER_SERVICE);
wakeLock = this.powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Lock");
//是否需计算锁的数量
wakeLock.setReferenceCounted(false);
 
//请求常亮,onResume()
wakeLock.acquire();
 
//取消屏幕常亮,onPause()
wakeLock.release();


 
方法二:
直接在onCreate()里,不用权限

复制代码 代码如下:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);