Running an android application on top of the lock screen

4k views Asked by At

I am trying to create an alarm clock that plays a Youtube video at a certain time. When I use wakelock, it plays the Youtube video behind the locked screen with this code:

PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "")

I would like the application to wake up the phone and play the Youtube video in front of the lock screen like the native alarm clock application rings in front of the locked screen.

Does anyone know how to do this? Thanks!

2

There are 2 answers

1
JiTHiN On

You can create a system overlay and play the video on it. Create a service and add this code:

@Override
    public void onCreate() {
        super.onCreate();
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
               |WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.RIGHT | Gravity.TOP;
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.addView(yourView, params);
    }

Dont forget to add SYSTEM_ALERT_WINDOW permission inside manifest.

EDIT: You can also experiment with TYPE_SYSTEM_ALERT and TYPE_SYSTEM_ERROR instead of TYPE_SYSTEM_OVERLAY.

0
Ed_ On

You can request than an activity is shown even when the screen is locked out sleeping, add this to onCreate:

this.getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);