Android Studio:Unable to add window android.view.ViewRootImpl$W@5e2d85a -- permission denied for thi

Posted sevenun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio:Unable to add window android.view.ViewRootImpl$W@5e2d85a -- permission denied for thi相关的知识,希望对你有一定的参考价值。

学习《第一行代码》的时候,出现的错误。

 

java.lang.RuntimeException: Unable to start receiver com.example.sevenun.littledemo.receiver.ForceOfflineReceiver: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@5e2d85a -- permission denied for this window type
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2732)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@5e2d85a -- permission denied for this window type
at android.view.ViewRootImpl.setView(ViewRootImpl.java:591)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:319)
at com.example.sevenun.littledemo.receiver.ForceOfflineReceiver.onReceive(ForceOfflineReceiver.java:38)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725)
at android.app.ActivityThread.-wrap14(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

 

因为《第一行代码》里面的程序主要是在Android4.x运行的,而最新的Android 6 对程序的权限有了新的要求。

所以,我们要启动类型为WindowManager.LayoutParams.TYPE_SYSTEM_ALERT的AlertDialog的时候:

 

如果是在Android4.x的情况,只用在AndroidManifest.xml里面声明<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />。

 

如果是在Android 6(API 23)的情况下,则可以添加以下代码进行请求权限,让用户同意后才可以弹出AlertDialog。

   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        if (Build.VERSION.SDK_INT >= 23) {
            if (! Settings.canDrawOverlays(LoginActivity.this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent,10);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 10) {
            if (Build.VERSION.SDK_INT >= 23) {
                if (!Settings.canDrawOverlays(this)) {
                    // SYSTEM_ALERT_WINDOW permission not granted...
                    Toast.makeText(LoginActivity.this,"not granted",Toast.LENGTH_SHORT);
                }
            }
        }
    }

  

 

参考:http://blog.csdn.net/chenlove1/article/details/52047105

以上是关于Android Studio:Unable to add window android.view.ViewRootImpl$W@5e2d85a -- permission denied for thi的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio:Unable to add window android.view.ViewRootImpl$W@5e2d85a -- permission denied for thi

[Visual Studio] - Unable to launch the IIS Express Web server 问题之解决

Android异常篇 Fail to connect to camera service

xbox android 蓝牙,How to Connect the Xbox Series X or S Controller to Android

Android下拉刷新控件android-Ultra-Pull-To-Refresh 使用

Android 异常: failed to connect to localhost/127.0.0.1