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

时间:2021-09-20 11:48:39

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

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);
}
}
}
}

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

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