WindowManager $ BadTokenException无法添加窗口

时间:2013-08-23 10:28:17

标签: android android-camera android-windowmanager

我正在为背景录像开发应用程序,这就是为什么我使用WindowManager,但它对我没有用。以下错误:

08-23 15:38:21.021: E/AndroidRuntime(4200): java.lang.RuntimeException: Unable to create service com.example.prankapp.BackgroundVideoRecorder: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@418c0b68 -- permission denied for this window type
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2277)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at android.app.ActivityThread.access$1600(ActivityThread.java:128)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1215)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at android.os.Handler.dispatchMessage(Handler.java:99)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at android.os.Looper.loop(Looper.java:137)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at android.app.ActivityThread.main(ActivityThread.java:4517)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at java.lang.reflect.Method.invokeNative(Native Method)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at java.lang.reflect.Method.invoke(Method.java:511)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at dalvik.system.NativeStart.main(Native Method)
    08-23 15:38:21.021: E/AndroidRuntime(4200): Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@418c0b68 -- permission denied for this window type
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:707)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at com.example.prankapp.BackgroundVideoRecorder.onCreate(BackgroundVideoRecorder.java:53)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2267)
    08-23 15:38:21.021: E/AndroidRuntime(4200):     ... 10 more

我的windowManager代码:

windowManager = (WindowManager) this.getSystemService(BackgroundVideoRecorder.WINDOW_SERVICE);
            surfaceView = new SurfaceView(this);
            LayoutParams layoutParams = new WindowManager.LayoutParams(
                1, 1,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT
            );
            //layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
            windowManager.addView(surfaceView, layoutParams);
            surfaceView.getHolder().addCallback(this);

请帮帮我。谢谢。

3 个答案:

答案 0 :(得分:22)

08-23 15:38:21.021: E/AndroidRuntime(4200): Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@418c0b68 -- permission denied for this window type

问题:清单文件中缺少权限。 解决方案:AndroidManifest中使用以下权限。

android.permission.SYSTEM_ALERT_WINDOW

答案 1 :(得分:10)

如果您的申请char,请勿使用

WindowManager.LayoutParams.TYPE_PHONE或WindowManager.LayoutParams.TYPE_SYSTEM_ALERT

你可以使用

apiLevel >= 19

LayoutParams.TYPE_TOAST

现在我的LayoutParams代码就像,

TYPE_APPLICATION_PANEL

答案 2 :(得分:0)

从棉花糖版本开始,许可批准有点复杂。因此,即使在给予上述许可后,此问题仍未得到解决。请参阅以下链接。

Unable to add window android.view.ViewRoot$W@44da9bc0 -- permission denied for this window type