默认锁定屏幕上的 Android 活动

我怎样才能显示一个 ActivityDialog可见的锁定屏幕?

我已经试过在屏幕打开时显示我的锁定活动,方法是在 Activity.onCreate()方法中设置不同的窗口类型:

TYPE_PRIORITY_PHONE
TYPE_SYSTEM_ALERT
TYPE_KEYGUARD

和其他与 SYSTEM_ALERT_WINDOWINTERNAL_SYSTEM_WINDOW权限一起。

解锁设备后,我的活动可见。

更新:

我实际上已经设法显示了我自己的活动,而不是默认的锁定屏幕。它工作完美,除非你使用主页按钮。

87833 次浏览
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);

try using this flags to disable lock screen when the activity is started.

After API level 17 you can use

<activity
android:name=".yourActivityName"
android:showOnLockScreen="true"
android:screenOrientation="sensorPortrait" >

showOnLockScreen like in the example...

Use this in onCreate method

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
// This line should be before setContentView..
setContentView(......);

Hope this will work Thanks

Don't go for activity, because android will not show lock screen behind your activity for security reason, so use service instead of Activity.

Below is my code in onStartCommand of my service.

WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);


View mView = mInflater.inflate(R.layout.score, null);


WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
/* | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON */,
PixelFormat.RGBA_8888);


mWindowManager.addView(mView, mLayoutParams);

And add <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> to manifest

You can use code in any of answers here that you think it's working. Then to prevent HOME button to work, change TYPE_SYSTEM_ALERT or TYPE_SYSTEM_OVERLAY (depends on what you currently use) to TYPE_SYSTEM_ERROR:

params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,
PixelFormat.TRANSLUCENT
);
params.gravity = Gravity.TOP;
mOverlay = (RelativeLayout) inflater.inflate(R.layout.main, (ViewGroup) null);
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mWindowManager.addView(mOverlay, params);