Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FLAG_TURN_SCREEN_ON doesn't work

I have a service with registered accelerometer inside. When certain shake pattern is recognized the service starts one activity using this code.

Intent launchIntent = new Intent("my.package.MAIN_ACTIVITY");
LaunchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(LaunchIntent);

At onCreate I use this code snippet to unlock the phone and turn the screen on:

Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON|
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

If the user don't interact with activity some time I use this code to enable the display to turn off:

Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

If the display turns off and phone locks while activity is displayed and I repeat the shake pattern the onStart method is called. I tried to put the same code for turning on and unlocking as above, but it doesn't work (display is not turned on).

What could be the problem?

like image 383
sinisha Avatar asked Nov 19 '13 19:11

sinisha


1 Answers

I managed to solve the problem. When starting my activity I use WakeLock:

PowerManager pm = ((PowerManager) getSystemService(POWER_SERVICE));
screenLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
screenLock.acquire();

This code will prevent the screen to turn off. But after that I start a timer with delay of few seconds and it disables the WakeLock:

if(screenLock.isHeld()) {
    screenLock.release();
}
like image 173
sinisha Avatar answered Sep 28 '22 00:09

sinisha