How To Show Activity On Lock Screen When Password Is Set In Android
Asked Answered
D

1

9

I am trying to show an activity when the screen is locked. I am running a service in the background, when an event occurs, I want to open an activity even if the app is locked(Similar to alarm app, which wakes up the screen and displays its activity). I have followed the following steps,

When OnReceive() is called, I want to open the activity on the lock screen.

public void OnReceive() {
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "myalarmapp:alarm.");
            wl.acquire(5000);

            Intent startAlarmActivity = new Intent(MainScreen.this, AcceptScreen.class);

            startActivity(startAlarmActivity);

            wl.release();
        }

Added below code in the activity's onCreate method which I want to show,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {

        setShowWhenLocked(true);
        setTurnScreenOn(true);
        KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        keyguardManager.requestDismissKeyguard(this, null);
    }
    else {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

Added this in manifest for the activity which I want to show,

<activity
        android:name=".v2.ui.orderaccept.AcceptScreen"
        android:exported="true"
        android:label="@string/title_activity_accept_screen"
        android:theme="@style/AppTheme.NoActionBar"
        android:showOnLockScreen="true"
        android:screenOrientation="sensorPortrait"/>
       

It is working as expected when the phone does not have a lock screen password. But it does not work when there is a password for the lock screen.

Dix answered 20/1, 2021 at 12:13 Comment(0)
D
12

Finally, I got the solution.

add below code in the activity's onCreate method which you want to show on the lock screen,

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

Manifest,

<activity
        android:name=".v2.ui.orderaccept.AcceptScreen"
        android:exported="true"
        android:label="@string/title_activity_accept_screen"
        android:theme="@style/AppTheme.NoActionBar"
        android:launchMode="singleInstance"/>

Call the activity,

val intent = Intent(context, targetclass)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        startActivity(intent)
    
Dix answered 22/1, 2021 at 7:15 Comment(3)
Does this work on the latest SDK?Cnidoblast
Thank you so much! This helped me a lot. Google really makes developers jump through hoops to figure out how to do such a fundamental thing.Creekmore
Is this work? It's not work in lock screen.Cloying

© 2022 - 2024 — McMap. All rights reserved.