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

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) 


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

如果是在Android4.x的情况,只用在AndroidManifest.xml里面声明

 最新的Android 6 对程序的权限有了新的要求。

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

   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@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);
             }
         }
     }
}

Android 6.0:Unable to add window android.view.ViewRootImpl$W@5e2d85a -- permission denied_第1张图片  Android 6.0:Unable to add window android.view.ViewRootImpl$W@5e2d85a -- permission denied_第2张图片

 


你可能感兴趣的:(Android知识)