zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    需求规格说明书
    团队作业—选题报告
    Department and Student
    软件工程第一次团队作业
    第二次作业——个人项目实战
    软件工程实践2017第一次作业
    第七次作业--项目需求分析
    玩下软工项目,第一轮--全局Context的获取,SQLite的建立与增删改查,读取用户通话记录信息
    软工实践结对编程第二次作业
    java--由一道选择题研究数值越界
  • 原文地址:https://www.cnblogs.com/sevenun/p/5858704.html
Copyright © 2011-2022 走看看