广播最佳实践---实现强制下线

今天在做广播最佳实践---实现强制下线小例子时,出现如下异常

java.lang.RuntimeException: Unable to start receiver com.lvkui.boardcast_forceoffline.ForceOfflineReceiver: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@100c0fab -- permission denied for this window type
            at android.app.ActivityThread.handleReceiver(ActivityThread.java:2586)
            at android.app.ActivityThread.access$1700(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@100c0fab -- permission denied for this window type
            at android.view.ViewRootImpl.setView(ViewRootImpl.java:586)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:298)
            at com.lvkui.boardcast_forceoffline.ForceOfflineReceiver.onReceive(ForceOfflineReceiver.java:33)
            at android.app.ActivityThread.handleReceiver(ActivityThread.java:2579)
            at android.app.ActivityThread.access$1700(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

额,没有运行成功。解决方法是:

manifest.xml 添加权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

< uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" /> 

posted on 2015-07-07 09:24  kuiblog.com  阅读(226)  评论(0编辑  收藏  举报