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

I am trying to start an action from a service that I already purchased to block, 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 phenomenon of activity 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 onAttachedToWindow() (I tried both), 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 is not displayed. Instead, a lock screen is displayed. Each subsequent call to startActivity() works correctly, but I cannot find the reason for this odd behavior.

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

  • Android activity is not displayed when the screen accelerates and the screen lock does not turn off
  • How to unlock the screen when calling BroadcastReceiver?
  • Programmatically enable screen in android
  • Android Galaxy S4 - activity that is visible on the lock screen

Please note that my topic is not a dialog, but a full-screen one.

Any other ideas?

+7
android android-activity
source share
3 answers

I ran into the same problem, after doing a lot of searching here and google, found this , which unlocked the screen and pulled out my activity but it only works for me when the application 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 activity when the application is closed ... (using the broadcast receiver)

in documents (like here ), and most responses to SO flags are added as follows:

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

but when I tried, as it was in the example, it unlocked the screen instead of just turning on the screen.

hope this help. he still did not completely solve my problem.

EDIT:

found this article that solved my problem.

there is a comment where the dialogue topic is NOT used, which solved it for me

+7
source share

Since my application already includes Service , this is what I do: if the screen is locked , I register a broadcast receiver (a little easier than this , since it only reacts to unlocking), which starts the action as soon as the screen unlocks.

0
source share
 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. 

This can be implemented if you want to open a specific screen when a notification appears, for example, by calling skype.

0
source share

All Articles