Dim screen while user inactive

两盒软妹~` 提交于 2019-12-30 08:16:11

问题


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?


回答1:


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.




回答2:


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.




回答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; 


来源:https://stackoverflow.com/questions/4803248/dim-screen-while-user-inactive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!