• Android Studio:Unable to add window android.view.ViewRootImpl$W@5e2d85a -- permission denied for this window 第一行代码


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

    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

  • 相关阅读:
    [转]Angular4首页加载慢优化之路
    [转]angular 禁止缓存
    微服务之分布式跟踪系统(springboot+pinpoint)
    分布式追踪系统设计与实现
    QLExpress 规则引擎使用介绍
    几个常见规则引擎的简单介绍和演示
    大数据对账
    美团配送资金安全治理之对账体系建设
    美的支付-对账系统实现
    redis实现对账(集合比较)功能
  • 原文地址:https://www.cnblogs.com/sevenun/p/5858704.html
Copyright © 2020-2023  润新知