我正在使用叠加权限在我的应用中显示某些信息。在API 23 - 25上运行它可以正常工作(根据
请求许可,授权等)Unable to add window android.view.ViewRoot$W@44da9bc0 -- permission denied for this window type)。 (非常感谢ceph3us!)
在API 26上尝试相同的操作我收到一个错误,基本上“调用
时窗口类型2002的权限被拒绝”windowManager.addView(frameLayout, params);
Google是否改变了方式,叠加效果如何?任何想法,如何将我的文本作为叠加到Android 8(奥利奥),API 26的屏幕上? 谢谢你的想法!
这是错误日志:
08-24 16:41:56.730 2615-2615/net.zwittscha.testoverlay E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.zwittscha.testoverlay, PID: 2615
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.zwittscha.testoverlay/net.zwittscha.testoverlay.MainActivity}:
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@6fa0089 --
permission denied for window type 2002
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.view.WindowManager$BadTokenException:
Unable to add window android.view.ViewRootImpl$W@6fa0089 --
permission denied for window type 2002
at android.view.ViewRootImpl.setView(ViewRootImpl.java:789)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:92)
at net.zwittscha.testoverlay.MainActivity.createOnTopView(MainActivity.java:46)
at net.zwittscha.testoverlay.MainActivity.checkDrawOverlayPermission(MainActivity.java:66)
at net.zwittscha.testoverlay.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
在我的清单中,我有:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION"/>
这是我的MainActivity:
public class MainActivity extends AppCompatActivity {
DrawView dv;
FrameLayout frameLayout;
WindowManager windowManager;
LayoutInflater layoutInflater;
/** code to post/handler request for permission */
public final static int REQUEST_CODE = 1234;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkDrawOverlayPermission();
}
public void createOnTopView() {
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER;
if (frameLayout == null) frameLayout = new FrameLayout(getApplicationContext());
if (dv == null) dv = new DrawView(getApplicationContext());
windowManager = (WindowManager)
getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
windowManager.addView(frameLayout, params);
windowManager.addView(dv, params);
layoutInflater = (LayoutInflater)
getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Here is the place where you can inject whatever layout you want.
layoutInflater.inflate(R.layout.activity_main, frameLayout);
}
public void checkDrawOverlayPermission() {
/* check if we already have permission to draw over other apps */
if (android.os.Build.VERSION.SDK_INT > 22) {
if (!Settings.canDrawOverlays(this)) {
/* if not construct intent to request permission */
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
/* request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE);
}
else {
createOnTopView();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
/* check if received result code
is equal our requested code for draw permission */
if (requestCode == REQUEST_CODE && android.os.Build.VERSION.SDK_INT > 22) {
/* if so check once again if we have permission */
if (Settings.canDrawOverlays(this)) {
createOnTopView();
}
}
}
}
这是DrawView:
public class DrawView extends View {
int w;
int h;
int r;
float screenFactor;
TextPaint startTextPaint;
public DrawView(Context activity) {
super(activity);
}
@Override
protected void onSizeChanged(int width, int height, int oldw, int oldh) {
w = width;
h = height;
r = w / 2;
screenFactor = (r / 160f);
if (startTextPaint == null) startTextPaint = new TextPaint();
startTextPaint.setTextSize(100);
startTextPaint.setTextAlign(Paint.Align.CENTER);
startTextPaint.setTypeface(Typeface.create("Roboto Condensed", Typeface.BOLD));
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
startTextPaint.setARGB(255,255,0,0);
canvas.drawText("Test", w / 2, h / 2, startTextPaint);
}
}
答案 0 :(得分:42)
根据针对Android 8.0的应用的Android 8.0 Behavior Changes文档:
使用SYSTEM_ALERT_WINDOW权限的应用无法再使用以下窗口类型在其他应用和系统窗口上方显示警报窗口:
TYPE_PHONE
TYPE_PRIORITY_PHONE
TYPE_SYSTEM_ALERT
TYPE_SYSTEM_OVERLAY
TYPE_SYSTEM_ERROR
相反,应用必须使用名为
TYPE_APPLICATION_OVERLAY
的新窗口类型。
因此,您的应用可以针对某些较低版本。在这种情况下,您的警报窗口将......
始终显示在使用TYPE_APPLICATION_OVERLAY窗口类型的窗口下方。如果某个应用面向Android 8.0(API级别26),该应用会使用TYPE_APPLICATION_OVERLAY窗口类型来显示警报窗口。
(引自同一来源)
答案 1 :(得分:20)
Android Oreo(及未来版本)不允许使用WindowManager.LayoutParams.TYPE_PHONE因为不推荐使用WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
}else{
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
}
}
答案 2 :(得分:2)
int LAYOUT_FLAG;
mOverlayView = LayoutInflater.from(this).inflate(R.layout.overlay_layout, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
答案 3 :(得分:1)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR
}
else
{
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
}
仅更改窗口管理器的此参数
答案 4 :(得分:0)
使用以下代码
int LAYOUT_FLAG = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_PHONE;
mWindowParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG, // Overlay over the other apps.
LayoutParams.FLAG_NOT_FOCUSABLE // This flag will enable the back key press.
| LayoutParams.FLAG_NOT_TOUCH_MODAL, // make the window to deliver the focus to the BG window.
PixelFormat.TRANSPARENT);
答案 5 :(得分:0)
您必须直接请求类似这样的权限(在我的项目主页中为目标页面),而不是直接进入您的页面 您可以在FIX PERMISSION DENIED FOR THIS WINDOW TYPE
中找到更多信息private static final int OVERLAY_PERMISSION_CODE =5463 ; // can be anything
@Override
public void onCreate(...) {
if (Build.VERSION.SDK_INT >= 23) {
if (!Settings.canDrawOverlays(this)) {
// Open the permission page
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, OVERLAY_PERMISSION_CODE);
return;
}
}
}