The error that occurred while learning the first line of code.
Java.lang.RuntimeException:Unable to start receiver Com.example.sevenun.littledemo.receiver.ForceOfflineReceiver: android.view.windowmanager$badtokenexception:unable to add window [e-mail protected]--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 [e-mail protected]--permission denied for T His 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)
Because the program in the first line of code is mostly run in android4.x, the latest Android 6 has new requirements for the program's permissions.
So, we're going to start with the type WindowManager.LayoutParams.TYPE_SYSTEM_ALERT alertdialog:
If it is in the case of android4.x, use only androidmanifest.xml inside the declaration <uses-permission android:name= "Android.permission.SYSTEM_ALERT_ Window "/>.
If you are in Android 6 (API 23), you can add the following code to request permission, let the user agree before you can eject Alertdialog.
@Override protected void OnCreate (Bundle savedinstancestate) {Super.on Create (savedinstancestate); Setcontentview (R.layout.activity_login); if (Build.VERSION.SDK_INT >=) {if (! Settings.candrawoverlays (Loginactivity.this)) {Intent Intent = new Intent (settings.action_manage_overlay_p Ermission, 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 >=) {if (! Settings.candrawoverlays (This)} {//System_alert_window permission not granted ... T Oast.maketext (Loginactivity.this, "not granted", toast.length_short); } } } }
Reference: http://blog.csdn.net/chenlove1/article/details/52047105
Android studio:unable to add window [e-mail protected] --Permission denied for this window first line of code