Android保持屏幕常亮2种实现方法
程序员文章站
2022-07-03 18:56:43
方法一:
复制代码 代码如下:
<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);
方法一:
复制代码 代码如下:
<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);