// 最大的屏幕亮度 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); } };
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。