我需要显示带有主叫号码文本视图的浮动窗口 在锁定屏幕上正常工作,并在较低的API(大于26)上解锁 从服务运行代码时,如何在API 26及更高版本的锁定屏幕上显示浮动窗口的正确方法?
此代码适用于API 26+的解锁屏幕
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
windowParams2 = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
windowParams2.x = 0;
windowParams2.y = 0;
windowParams2.gravity = Gravity.CENTER;
}else{
windowParams2 = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE ,
PixelFormat.TRANSLUCENT);
windowParams2.x = 0;
windowParams2.y = 0;
windowParams2.gravity = Gravity.CENTER;
}
答案 0 :(得分:-1)
添加以下标志:
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
也不要忘记您是否希望它正常工作,您需要为清单添加一些权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />