不要进行活动,因为出于安全原因,Android不会在活动后显示锁屏,因此请使用服务而不是活动。
以下是我在服务的onStartCommand中的代码。
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
,
PixelFormat.RGBA_8888);
mWindowManager.addView(mView, mLayoutParams);
并添加<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
到清单