在 API 26 及更高版本中,在锁定屏幕上从服务中浮动 window
Floating window from service over locked screen in API 26 and higher
我需要在主叫号码上显示带有文本视图的浮动 window
这在锁定屏幕上工作正常并在较低 API(低于 26)下解锁
在锁定屏幕上 API 26 和更高版本上显示浮动 window 而 运行 来自服务的代码的正确方法是什么?
此代码适用于 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;
}
添加以下标志:
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" />
我需要在主叫号码上显示带有文本视图的浮动 window 这在锁定屏幕上工作正常并在较低 API(低于 26)下解锁 在锁定屏幕上 API 26 和更高版本上显示浮动 window 而 运行 来自服务的代码的正确方法是什么?
此代码适用于 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;
}
添加以下标志:
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" />