4

I'm using the following code to keep the screen on :

 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

My only problem is that I want to continue using the dimming screen to save battery. I know how to do that using WakeLock, but is there a way to do that without it?

The Dark Knight
  • 5,455
  • 11
  • 54
  • 95
Victor Laerte
  • 6,446
  • 13
  • 53
  • 102
  • Why do u wanna do it without it ? Are there any problems while using wakelock ? – The Dark Knight Apr 29 '13 at 13:48
  • 1
    Wakelock is deprecated. As you said "people don't use the wakelock facility as this requires that you give your app an additional permission, and it is very easy to introduce bugs" – Victor Laerte Apr 29 '13 at 14:43
  • Seems like [this](https://stackoverflow.com/a/22447615/1562087) is what you looking for... – KenIchi Jan 10 '19 at 01:16

3 Answers3

0

Normally, people don't use the wakelock facility as this requires that you give your app an additional permission, and it is very easy to introduce bugs where you accidentally remain holding the wake lock and thus leave the screen on. Hence it is better to use the window flag FLAG_KEEP_SCREEN_ON, which you can enable on your activity's window .

Another solution is to add android:keepScreenOn="true" (documentation) to the views that need to keep the screen on. This will allow you for a little bit more granular control in terms of which views stay on and which don't. You can even reference a setting from a resource file this way.

A related method setKeepScreenOn(true) can be used for this purpose as well .

The Dark Knight
  • 5,455
  • 11
  • 54
  • 95
0

I'm not sure if this method saves battery but you could create an invisible activity and remove touch input for it and set it's dim like this:

wm = (WindowManager) getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mTopView = (ViewGroup) inflater.inflate(
                R.layout.activity_invisible, null);
        params.flags =  WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                | WindowManager.LayoutParams.FLAG_DIM_BEHIND
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.dimAmount=0.3f;//0 is transparent 1 is opaque
rl = (RelativeLayout) mTopView.findViewById(R.id.window);
        getWindow().setAttributes(params);
        wm.addView(mTopView, params);}

I've tested the code in one of my applications. Should work.

Karthik Balakrishnan
  • 4,353
  • 6
  • 37
  • 69
0

You can clear the flags that you programmatically set using the clearFlag() method

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

This will allow the screen to go dim again.

Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • I understand that this is a very old question, but hopefully the above solution might be some help to people who stumble upon this question in the future! – Antrromet May 26 '15 at 00:52
  • This seems like it will allow the screen to go off again... It seems like the OP wanted a replacement for SCREEN_DIM_WAKE_LOCK... – Gert van den Berg Apr 26 '17 at 11:18