// 最大的屏幕亮度
private float maxLight;
// 当前的亮度
private float currentLight;
// 用来控制屏幕亮度
private Handler lightHandler;
// 60秒时间不点击屏幕,屏幕变暗
private long delayTime = 60 * 1000L;
protected void onCreate() {
// 保持屏幕常亮
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// 屏幕亮度控制
lightHandler = new Handler(Looper.getMainLooper());
maxLight = getLightness();
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (currentLight == 1) {
startSleepTask();
return false;
}
startSleepTask();
return super.dispatchTouchEvent(ev);
}
/**
* 获取亮度
*/
private float getLightness() {
WindowManager.LayoutParams localLayoutParams = this.getWindow().getAttributes();
return localLayoutParams.screenBrightness;
}
/**
* 设置亮度
*/
private void setLightness(int light) {
currentLight = light;
WindowManager.LayoutParams localLayoutParams = this.getWindow().getAttributes();
localLayoutParams.screenBrightness = (light / 255.0F);
this.getWindow().setAttributes(localLayoutParams);
}
/**
* 开启休眠任务
*/
private void startSleepTask() {
setLightness((int) maxLight);
stopSleepTask();
lightHandler.postDelayed(sleepWindowTask, delayTime);
}
/**
* 结束休眠任务
*/
private void stopSleepTask() {
lightHandler.removeCallbacks(sleepWindowTask);
}
/**
* 休眠任务
*/
Runnable sleepWindowTask = new Runnable() {
@Override
public void run() {
setLightness(1);
}
};
分享标题:直播带货源码Android屏幕常亮N秒后自动暗屏
文章来源:
http://bzwzjz.com/article/jojjhs.html