14

I am developing app for background videorecording ,thats why i used WindowManager,but it did not worked for me.gives following errors:

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

My windowManager code:

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);

Please help me.Thanks in advance.

Vasu
  • 886
  • 2
  • 11
  • 28

3 Answers3

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

Problem: Permission missing in maifest file.

Solution: Use following permission in AndroidManifest.

android.permission.SYSTEM_ALERT_WINDOW
Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
  • one activity,one service & following permissions ** ** – Vasu Aug 23 '13 at 11:22
  • Try android.permission.SYSTEM_ALERT_WINDOW instead of android:name="SYSTEM_ALERT_WINDOW" – Ritesh Gune Aug 23 '13 at 11:30
  • 1
    @MBH, maybe add permissions during runtime? http://stackoverflow.com/questions/7569937/unable-to-add-window-android-view-viewrootw44da9bc0-permission-denied-for-t – CoolMind Mar 10 '16 at 09:28
  • yup permissions has to be handled at runtime for marshmallow. This answer was posted for pre-marshmallow. Will update my answer in sometime. – Ritesh Gune Mar 15 '16 at 05:23
  • With marshmallo this issue persists. So to avoid please see the link http://stackoverflow.com/questions/7569937/unable-to-add-window-android-view-viewrootw44da9bc0-permission-denied-for-t#answer-34061521 – rajeesh Apr 28 '16 at 09:24
  • here is the latest working answer https://stackoverflow.com/questions/46208897/android-permission-denied-for-window-type-2038-using-type-application-overlay – Muhammad Natiq Dec 16 '18 at 17:44
10

if your application apiLevel >= 19, don't use

WindowManager.LayoutParams.TYPE_PHONE or WindowManager.LayoutParams.TYPE_SYSTEM_ALERT

you can use

LayoutParams.TYPE_TOAST

or

TYPE_APPLICATION_PANEL

now my code for LayoutParams is like,

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_TOAST,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
varotariya vajsi
  • 3,965
  • 37
  • 39
  • 1
    for Lollipop and lower I used WindowManager.LayoutParams.TYPE_PHONE, but It didn't work for Android M, then I used WindowManager.LayoutParams.TYPE_TOAST as you suggested and it works now, thanks! – Mu Sa Aug 08 '16 at 09:20
  • 1
    Yes, `TYPE_TOAST` works when you are not using to draw on lockscreen – Hitesh Chavda Sep 14 '16 at 04:39
  • I have to use `android.permission.SYSTEM_ALERT_WINDOW` ? – t0m Feb 21 '17 at 12:30
  • 1
    t0m: if you use the TYPE_TOAST, no permission needed – Arkady Apr 27 '17 at 13:14
  • `LayoutParams.TYPE_TOAST` is deprecated from API 26. Use `LayoutParams.TYPE_APPLICATION_OVERLAY` instead and it is requires `android.Manifest.permission#SYSTEM_ALERT_WINDOW` permission => [See more](https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#TYPE_TOAST) – t0m Jan 04 '18 at 14:32
  • I get `android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?` when using `WindowManager.LayoutParams.TYPE_TOAST` instead of `WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY` on Pixel emulator API 25, though it works ok for Android 23 emulator, weird... – user924 Apr 30 '23 at 10:29
0

Starting from marshmallow versions the permission approval is a little bit complex. So even after giving the above permission this issue is not resolved. Please see the following link.

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

Community
  • 1
  • 1
rajeesh
  • 937
  • 10
  • 11