Android Studio中窗口类型2002的权限被拒绝

时间:2018-08-28 13:23:44

标签: android android-permissions

今天我安装了最新版本的Android Studio

我正在学习Android中的Floating Widgets

我从应用此示例开始

https://www.spaceotechnologies.com/android-floating-widget-tutorial/

它可以编译

但是当我在模拟器中运行它会崩溃

给我这个错误

08-28 22:52:02.932 7400-7400/com.asmgx.MyApp.MyApp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.asmgx.MyApp.MyApp, PID: 7400
    java.lang.RuntimeException: Unable to create service com.asmgx.MyApp.MyApp.FloatWidgetService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@7c93828 -- permission denied for window type 2002
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3544)
        at android.app.ActivityThread.access$1300(ActivityThread.java:199)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@7c93828 -- permission denied for window type 2002
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:822)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
        at com.asmgx.MyApp.MyApp.FloatWidgetService.onCreate(FloatWidgetService.java:36)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3532)
        at android.app.ActivityThread.access$1300(ActivityThread.java:199) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

我尝试解决此问题并找到了此链接

Unable to add window android.view.ViewRoot$W@44da9bc0 -- permission denied for this window type

他们建议将此行添加到已经添加的清单中

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

有人知道我为什么要得到这个吗?

PS。我在Android 28上使用了模拟器,在Android 27上使用了另一个模拟器

4 个答案:

答案 0 :(得分:16)

之所以发生这种情况,是因为示例中的targetSdkVersion与您的targetSdkVersion不同。尝试将其减少到22,或在WindowManager.LayoutParams中使用标志TYPE_APPLICATION_OVERLAY而不是TYPE_PHONE:

WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY 

答案 1 :(得分:3)

通过将targetSdkVersion更改为22,该问题将得到部分解决。对于22以上的设备,您将得到相同的错误。 我在堆栈溢出中找到以下答案,但不记得链接了。您可以尝试以下代码:

private void createFloatView() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        checkDrawOverlayPermission();
    } else {
        createView();
    }
}

public void checkDrawOverlayPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(getContext())) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getActivity().getPackageName()));
            startActivityForResult(intent, CommonVariables.REQUEST_CODE);
        } else {
            createView();
        }
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CommonVariables.REQUEST_CODE) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.canDrawOverlays(getContext())) {
                createView();
            }
        }
    }
}

 private int CommonVariables.REQUEST_CODE = 5463 & 0xffffff00

答案 2 :(得分:2)

这对我有用。我敢肯定它可以简化,但在其他方面可以很好地工作。
如果(等于 )用户等于或高于Oreo,只需将TYPE_PHONE替换为TYPE_APPLICATION_OVERLAY

final WindowManager.LayoutParams params;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
    } else {
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
    }

答案 3 :(得分:0)

只需替换

TYPE_PHONE,

在您的代码中使用

android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O ? TYPE_APPLICATION_OVERLAY : TYPE_PHONE,