歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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”

可是帶進去一跑,還是不行,再找

Copyright © Linux教程網 All Rights Reserved