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:
Note that my theme is not a dialog but a fullscreen activity.
Any other ideas?
To enable it you have to go to the app configuration -> Other permissions -> Show On Lock screen.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With