您好,登錄后才能下訂單哦!
// 最大的屏幕亮度 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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。