23

Today I installed latest version of Android Studio

I am learning Floating Widgets in Android

I started with applying this example

https://www.spaceotechnologies.com/android-floating-widget-tutorial/

it compiles ok

but when I run it in the emulator it crashes

giving me this error

08-28 22:52:02.932 7400-7400/com.asmgx.MyApp.MyApp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.asmgx.MyApp.MyApp, PID: 7400
    java.lang.RuntimeException: Unable to create service com.asmgx.MyApp.MyApp.FloatWidgetService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@7c93828 -- permission denied for window type 2002
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3544)
        at android.app.ActivityThread.access$1300(ActivityThread.java:199)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@7c93828 -- permission denied for window type 2002
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:822)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
        at com.asmgx.MyApp.MyApp.FloatWidgetService.onCreate(FloatWidgetService.java:36)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3532)
        at android.app.ActivityThread.access$1300(ActivityThread.java:199) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

I tried resolving the issue and found this link

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

they suggested adding this line to the manifest, which already added

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

anyone know why am i getting this?

PS. I used emulator with Android 28 and another with android 27

InsaneCat
  • 2,115
  • 5
  • 21
  • 40
asmgx
  • 7,328
  • 15
  • 82
  • 143

4 Answers4

52

This is occurring because the targetSdkVersion in the example and your targetSdkVersion are different. Use the flag TYPE_APPLICATION_OVERLAY instead of TYPE_PHONE in WindowManager.LayoutParams:

WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY 
Venkata Narayana
  • 1,657
  • 12
  • 25
  • how can i experiment with different flags ? – asmgx Aug 28 '18 at 14:02
  • Try different flags like overlay – Venkata Narayana Aug 28 '18 at 15:14
  • Try the following flags: WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, TYPE_SYSTEM_OVERLAY, TYPE_ACCESSIBILITY_OVERLAY, – Venkata Narayana Aug 28 '18 at 16:25
  • It crashes on all other types. is there any other way i can get it working? – asmgx Aug 29 '18 at 08:21
  • TYPE_APPLICATION_OVERLAY ? – Venkata Narayana Aug 29 '18 at 08:37
  • Works :) Thanks heaps – asmgx Aug 29 '18 at 09:38
  • `TYPE_APPLICATION_OVERLAY` has been added in API 26 not 22 https://developer.android.com/reference/android/view/WindowManager.LayoutParams – Behrouz.M Jan 08 '19 at 19:12
  • 1
    You've misinterpreted the answer. Either downgrade the targetSdkVersion to 22 or keep it at 26 and change the flag to TYPE_APPLICATION_OVERLAY. Makes sense? – Venkata Narayana Jan 08 '19 at 20:53
  • I downgraded because this accepted answer is not clear enough, option to downgrade targetSdkVersion 22 is not good solution is this mostly you don't want to do, there should be different flags for different versions as is proposed in other answers. – Renetik Nov 17 '19 at 16:01
  • Downgrading the targetSDK verison is not a solution anymore and it is mandated by Google to have the latest version. Edited the answer to reflect that. Nevertheless, changing the flags like it was suggested in the answer should work – Venkata Narayana Nov 17 '19 at 23:54
  • `TYPE_APPLICATION_OVERLAY ` gave me `permission denied for window type 2038` on Android 10 and 11. `android.permission.SYSTEM_ALERT_WINDOW` has been requested in the manifest file. – user1801605 Jul 20 '21 at 02:37
23

This worked for me.. I'm sure it can be simplified, but it works perfectly otherwise.
Simply replace TYPE_PHONE with TYPE_APPLICATION_OVERLAY if (and only if) user is equal or above Oreo:

final WindowManager.LayoutParams params;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
    } else {
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
    }

OR do this in simple way

    int LAYOUT_FLAG;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
        }

        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                LAYOUT_FLAG,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
Yosidroid
  • 2,053
  • 16
  • 15
Studio2bDesigns
  • 578
  • 5
  • 12
4

By changing targetSdkVersion to 22, the issue will be solved partially. For devices above 22 you will get the same error. I found the following answer in stack overflow but didn't remember the link. You can try the following code:

private void createFloatView() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        checkDrawOverlayPermission();
    } else {
        createView();
    }
}

public void checkDrawOverlayPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(getContext())) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getActivity().getPackageName()));
            startActivityForResult(intent, CommonVariables.REQUEST_CODE);
        } else {
            createView();
        }
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CommonVariables.REQUEST_CODE) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.canDrawOverlays(getContext())) {
                createView();
            }
        }
    }
}

 private int CommonVariables.REQUEST_CODE = 5463 & 0xffffff00
Petros Mosoyan
  • 238
  • 2
  • 15
  • This is your missing link.. https://stackoverflow.com/questions/32224452/android-unable-to-add-window-permission-denied-for-this-window-type/47103959#47103959 – StarWind0 Aug 07 '19 at 22:09
1

Simply replace

TYPE_PHONE,

in your code with

android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O ? TYPE_APPLICATION_OVERLAY : TYPE_PHONE,
goodhyun
  • 4,814
  • 3
  • 33
  • 25