0

I am using this code to prevent dimming/locking screen, it is perfectly working for prevention of locking, but dimming effect is not prevented yet. please tell me some way out to prevent dimming also

protected void setScreenLock(boolean on){


  if(mWakeLock == null){
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK |
                                    PowerManager.ON_AFTER_RELEASE, TAG);
    }
    if(on){
     mWakeLock.acquire();
    }else{
        if(mWakeLock.isHeld()){
            mWakeLock.release();
        }
     mWakeLock = null;
    }

}
Hariharan
  • 24,741
  • 6
  • 50
  • 54
Shruti
  • 55
  • 7

2 Answers2

0

Add

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
View root = findViewById(android.R.id.content);
if (root != null)
    root.setKeepScreenOn(true);

to your if part. This won't allow the screen to be dimmed.

Apoorv
  • 13,470
  • 4
  • 27
  • 33
  • this is helpful, with this neither the screen is dimming nor locking, but what i have to make the screen again able to lock or dim – Shruti May 23 '14 at 06:27
  • Call `mWakeLock.release();` and `root.setKeepScreenOn(false);` when you want it to allow it to dim or lock. – Apoorv May 23 '14 at 06:29
  • public void setWindowDim(boolean dim) getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); View root = findViewById(android.R.id.content); if (root != null) { if (!dim) { root.setKeepScreenOn(true); } else { root.setKeepScreenOn(false); } } } – Shruti May 23 '14 at 06:32
  • now i m using this function, root.setKeepScreenOn(true) is working fine but root.setKeepScreenOn(false) is not working – Shruti May 23 '14 at 06:33
  • Are you passing dim as `true`? – Apoorv May 23 '14 at 06:34
  • NO, when i want to prevent dimming, m passing dim as false – Shruti May 23 '14 at 06:37
  • When you want to allow to dim you have to change `getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);` to `getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);` – Apoorv May 23 '14 at 06:40
0

Do not use WakeLock at all (remove your current code and you can also get rid of related permission from your Manifest file) - this is incorrect (in most cases) approach. Use FLAG_KEEP_SCREEN_ON flag in your onCreate() to tell framework to manage this for you. Doc reads:

Window flag: as long as this window is visible to the user, keep the device's screen turned on and bright.

like this:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • but with this screen will always remain on, what I should use to release the screen_on effect – Shruti May 23 '14 at 06:10
  • Screen will be only on when your activity is visible. If you put it i.e. in background, then screen will go off if needed without you doing anything. And here I found is the same answer from Android dev @Google: http://stackoverflow.com/a/2134602/1235698 – Marcin Orlowski May 23 '14 at 07:48