简体   繁体   中英

Changing screen brightness efficiently

I need to change the screen brightness programmatically. I read multiple solutions like this Can't apply system screen brightness programmatically in Android .

My problem is that those solutions implies changing the activity to be effective (having something like a dummy activity finishing immediately) and I would like to avoid the overhead of an activity switch.

Is there any other solution... maybe using native code so that the screen brightness will change immediately ?

The following affects immediately the single activity, no need to restart it. The activity also remembers the screenBrightness attribute over pause/resume.

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 1; // 0f - no backlight ... 1f - full backlight
getWindow().setAttributes(lp);

But it has no effect if you have automatic backlight level enabled in the system settings. This solution should help to turn off automatic backlight.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM