Android中后台显示悬浮窗口的方法

想照着音量对话框的做法,作一个在后台显示Dialog的方法,可是在Dialog.show()的时候,出了下面这个异样:

Android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@411da608 -- permission denied for this window type:
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:537)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
        at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
        at android.view.Window$LocalWindowManager.addView(Window.java:537)
        at android.app.Dialog.show(Dialog.java:278)

1、异常原因-没加权限

往下的调用顺序是

①    android.view.ViewRootImpl.setView(ViewRootImpl.java:481)
  ②    com.android.server.wm.Session.add(Session.java:139)
  ③    com.android.server.wm.WindowManagerService.addWindow(WindowManagerService:1999)
  ④    com.android.internal.policy.impl.PhoneWindowManager.checkAddPermission(PhoneWindowMana:1063)

在这里发现了

String permission = null;
        switch (type) {
            case TYPE_TOAST:
                // XXX right now the app process has complete control over
                // this...  should introduce a token to let the system
                // monitor/control what they are doing.
                break;
            case TYPE_INPUT_METHOD:
            case TYPE_WALLPAPER:
                // The window manager will check these.
                break;
            case TYPE_PHONE:
            case TYPE_PRIORITY_PHONE:
            case TYPE_SYSTEM_ALERT:
            case TYPE_SYSTEM_ERROR:
            case TYPE_SYSTEM_OVERLAY:
                permission = android.Manifest.permission.SYSTEM_ALERT_WINDOW;
                break;
            default:
                permission = android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
        }

音量对话框用的是“TYPE_VOLUME_OVERLAY”,那就加上“INTERNAL_SYSTEM_WINDOW”

可是带进去一跑,还是不行,再找

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c0644e0f7f03b8b09dc7f78fea36ac74.html