Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

android - 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:

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

Any other ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...