扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
// 最大的屏幕亮度 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); } };
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流