12

I have an app which is used for lets say 4 hours, but only every 5 minutes a user needs to make an input or read the screen. Putting the phone to sleep and locking the screen is a bit annoying. So I have two options:

  1. getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); which doesn't lock the screen but the screen is always bright, I would like to dim it while there is no activity. Just because of battery life span, or doesn't that matter that much for these 4 hours?

  2. a wake lock SCREEN_DIM_WAKE_LOCK which does as well what I want, but I was told rather to use option 1 above.

Can I achieve the wanted somehow without a wake lock?

Ry-
  • 218,210
  • 55
  • 464
  • 476
AndyAndroid
  • 4,039
  • 14
  • 44
  • 71

3 Answers3

14

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.

rjam
  • 577
  • 6
  • 19
  • What `dialog` object are you referring to here? From the docs it seems this field will only dim whatever is behind the window you are setting it on. So are you suggesting creating a dummy (perhaps invisible) window/dialog? – Tony Chan Oct 04 '13 at 23:19
  • Turbo, the dialog was just an actual dialog variable from my code, but you can call it right from the activity where you want to dim the screen (just remove the "dialog." from the last line)..I'll edit it out for clarity – rjam Oct 07 '13 at 14:06
  • I see. I was confused because of the wording in the [docs](http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DIM_BEHIND): _"Window flag: everything behind this window will be dimmed."_ So even though you apply this to the `Activity`'s `Window`, that `Activity` will still be dimmed, and not just what's behind it? That's why I thought you were making a dummy `Dialog` to apply it to so the `Activity` behind it would dim. But I guess it also affects the `Window` you're applying it to. – Tony Chan Oct 08 '13 at 20:34
  • Hmm, this code didn't work on my devices. Tested on Android 6.0 (Nexus 5X and Samsung S7) and Android 5.0 (Nexus 4). I tested floats of -1, 0, and 1 with no changes. I am able to retrieve the dimAmount of 1. – Anonsage May 19 '16 at 00:24
5

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.

hackbod
  • 90,665
  • 16
  • 140
  • 154
  • 1
    not bad, but it has a "funny effect": first I wait until the screen goes black, then I tap on the screen I am back in my activity. Perfect so far. I press a button which starts a new activity this brings up the lock screen either until I unlock it or if the second activity has the same flag set then only for half a second and then I am in the second activity. Neither what I want. If that lock screen wouldn't show up for a short moment this would be perfect, any idea how to achieve this? – AndyAndroid Jan 26 '11 at 15:28
3

Does this answer your last Q from Jan 26 '11 at 15:28

 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
 PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
  wl.acquire();
   ..screen will stay on during this section..
 wl.release();

See here for more: http://developer.android.com/reference/android/os/PowerManager.html

So what I mean is that you should acquire power managers wake lock before you launch your new activity to prevent screen lock. And release when you are done.


You could likewise use lp.screenBrightness instead of lp.dimAmount

And be sure to enter value higher then 0 to avoid screen lock:

lp.screenBrightness = 0.01f + value / 100.0f; 
errorau
  • 2,121
  • 1
  • 14
  • 38
Sold Out
  • 1,321
  • 14
  • 34