Dim screen while user inactive

You can use:

WindowManager.LayoutParams lp = getWindow().getAttributes();  
lp.dimAmount=1.0f; 
getWindow().setAttributes(lp);  

to dim/undim the screen when you want to.

lp.dimAmount=1.0f will dim the screen totally but you can set it at your will with values from 0 (no dim at all) to 1.0f.

You can set a timer to call this after 5 minutes of inactivity and then turn the screen "back on" on any touch event or such maybe.


Even a dim screen consumes a significant amount of battery over a time like 4 hours. You really don't want to keep the screen on like that. I think you would be better off using FLAG_SHOW_WHEN_LOCKED, allowing the screen to turn off, but for the user to immediately go in to your app when turning it back on without having to first go through the lock screen.

If you really need to keep the screen dim, you can use a SCREEN_DIM_WAKE_LOCK. If you want more control, you could also try directly setting the screen brightness with WindowManager.LayoutParams.screenBrightness.