问题
I have Activity1
which is a list screen of items. Each item can be viewed in a separate Activity2
which is displayed as a popup. Activity1
can be launched from the background and displayed even when the screen is locked. Activity1
may also choose to automatically display the contents of an item in the list screen by starting Activity2
. We can bypass the lock screen because both activities have the WindowManagerFlags.DismissKeyguard
enabled in the OnCreate
method.
Before Android Lollipop everything worked as expected. But now the popup Activity2
is not visible unless the device is manually unlocked. If I change Activity2
to be a full screen Activity
then everything seems to work (Except transitioning from one activity to another will briefly display the lock screen). Any ideas on how to fix this cleanly?
Also, I have only tried the Galaxy S6/S6 Edge devices which have this new Knox security feature on them.
Edit I have changed Activity2
to be a DialogFragment
instead of an Activity
. This worked for me best because the suggested answer used code that is deprecated or obsolete depending on the target sdk. Activity1
is using the following flags to bypass the lock screen when needed.
getWindow().addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Since Activity2
is now just a DialogFragment
, it uses the window flags of the parent Activity1
. I also remove those flags on the "android.intent.action.SCREEN_OFF" action so that the activity bypasses the lock screen only when launched as a notification and not every time the activity is at the top of the stack. Permissions mentioned in the answer are required.
回答1:
For sure this snippet should help You (works with Lollipop):
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Tag");
wl.acquire();
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();
and when leaving Your activity (i.e. onStop(), onPause() and onDestroy()):
keyguardLock.reenableKeyguard();
Also, don't forget about permissions:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
来源:https://stackoverflow.com/questions/29995138/android-lollipop-bypass-lock-screen-for-popup-activity