Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting activity from service on lock screen turns on the screen but does not show the activity itself

I'm trying to start an activity from a service I had already acquired the lock for as follows:

Intent i = new Intent(context, MyActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
startActivity(i);

The activity manifest is declared as follows:

<activity
        android:name=".MyActivity"
        android:configChanges="orientation|screenSize|keyboardHidden|keyboard|navigation"
        android:excludeFromRecents="true"
        android:launchMode="singleInstance"
        android:screenOrientation="nosensor"
        android:showOnLockScreen="true"
        android:taskAffinity=""
        android:theme="@style/MyTheme" />

And finally, on onCreate() or on onAttachedToWindow() (I tried on both), I add the following 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);

The problem is that the first time I call startActivity() from my service, the screen turns on but the activity itself does not show up. It shows the lock screen instead. Every subsequent call of startActivity() works properly but I can't find a reason for this odd behavior.

I tried already suggestions to get a full wakelock instead of partial, change the flags and values in the manifest according to the following SO answers:

  • Android Activity Not Showing When Screen is Woken Up and Lock Screen Not Disabling
  • how to unlock the screen when BroadcastReceiver is called?
  • Programmatically turn screen on in android
  • Android Galaxy S4 -- Activity that is visible over lock screen

Note that my theme is not a dialog but a fullscreen activity.

Any other ideas?

like image 801
Ricardo Avatar asked Dec 26 '13 13:12

Ricardo


People also ask

How do I show activity on lock screen?

To enable it you have to go to the app configuration -> Other permissions -> Show On Lock screen.

How do I show activity on lock screen instead of notification?

It's advised to use NotificationManager. IMPORTANCE_HIGH for increasing the chances of the notification appearing as a heads up notification. We also change the lockscreenVisibility to Notification. VISIBILITY_PUBLIC to tell Android that the notification can be shown on the lock screen.

Why my lockscreen is not working?

Open Settings and go to Security & fingerprint. Once inside, click on Smart Lock. Enter your screen lock pattern and if it is not enabled, then do it because you can't use Smart Lock without a pattern, pin or password.


2 Answers

Step 1: Add below code in your activity before

setContentView(R.layout.activity_about_us);

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

Step 2: Lock your mobile than you will see activity in which you have added this code.

You can implement this if you want to open particular screen by notification occurrence like skype call.

like image 79
shailesh Rohit Avatar answered Nov 15 '22 20:11

shailesh Rohit


I'm facing the same problem, after a lot of searching here and google, found this which unlocked the screen and popped my activity but it only works for me when the app is running (foreground/background).

import android.view.Window;
import android.view.WindowManager.LayoutParams;


Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

i'm trying to start an activty when app is closed... (using broadcast receiver)

in the docs (for example here) and most of the answers on SO the flags are added this way:

 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 

but when i tried the way it is like in the example it unlocked the screen instead of just turning on the screen.

hope this help . it still didn't solve my problem completely.

EDIT:

found this post which solved my problem.

there is a comment there on NOT using a dialog theme which solved it for me

like image 45
Guy S Avatar answered Nov 15 '22 21:11

Guy S