Question

I making the application where an activity launch is scheduled by AlarmManager. I would like to appear even if the screen is turned off and device is locked.

To achive this a set the Window flags

final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
        | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

And try to obtain lock for the screen

if(_lock == null)
{
    PowerManager pm = (PowerManager)App.getAppContext()
            .getSystemService(Context.POWER_SERVICE);

    _lock = pm.newWakeLock(
            PowerManager.FULL_WAKE_LOCK, "ScreenOn");
    _lock.acquire();
}

The _lock is PowerManager.WakeLock which is released in onPause

protected void onPause()
{
     if(_lock != null)
     {
          _lock.release();
     }
}

This code is executed in onCreate and onRestart. Everything works OK if the activity is not launched yet.

But if it was launched earlier the screen is not turned off.

  • onRestart is called first
  • onResume is then called
  • onPause is called immediately

So the activity is not launched. My question is how to turn on the screen in such situation. (I am using API 15).

Was it helpful?

Solution

I came up with the solution. I created a new activity which will be trying to turn on the screen in the onCreate() and then wait until it is turned on. When the screen is ok it will launch the activity which should be displayed. To make the Android always create this activity

public class TurnOnScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        if (pm.isScreenOn()) openActivity();
        else {
            registerReceiver(mScreenOnReceiver, new IntentFilter(
                    Intent.ACTION_SCREEN_ON));
            reciever_registered = true;
            turnScreenOn();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (reciever_registered) {
            unregisterReceiver(mScreenOnReceiver);
            reciever_registered = false;
        }
    }

    private boolean reciever_registered = false;
    private final BroadcastReceiver mScreenOnReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            openActivity();
        }
    };

    private void openActivity() {
        Intent intent = new Intent();
        // ....
        finish();
    }

    private void turnScreenOn() {
        final Window win = getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
    }
}

I am still looking for explanations why the screen is not turned on in onRestart.

OTHER TIPS

Just use your code :

final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

in onCreate() only and remove all those other Activity-Cycle methods if they are not doing anything else then this.

I don't think you need any more code to use to perform it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top