Mano
Mano

Reputation: 99

Android: permission denied for window type 2038

I trying to create float overlaying space in android. I have:

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

I registered a service with

<service
    android:name=".service.RecordService"
    android:enabled="true"
    android:foregroundServiceType="mediaProjection">
</service>

and run it with

                Intent intent = new Intent(MainActivity.this, RecordService.class);
                intent.putExtra("cmd","start service");
                startService(intent);
                mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, intent);

and all of it breaking on

        mWindowManager = (WindowManager)getApplication().getSystemService(WINDOW_SERVICE);
        mLayoutParams = new LayoutParams();
        mLayoutParams.type = LayoutParams.TYPE_APPLICATION_OVERLAY;
        mLayoutParams.format = PixelFormat.RGBA_8888;
        mLayoutParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
        mLayoutParams.gravity = Gravity.CENTER;
        mLayoutParams.x = 0;
        mLayoutParams.y = 0;
        mLayoutParams.width = LayoutParams.WRAP_CONTENT;
        mLayoutParams.height = LayoutParams.WRAP_CONTENT;

        final LayoutInflater inflater = LayoutInflater.from(getApplication());
        mFloatLayout = (FrameLayout)inflater.inflate(R.layout.layout_record,null);
        mStartBtn = (TextView)mFloatLayout.findViewById(R.id.btn_start);
        mStartBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RecordService.this, RecordService.class);
                intent.putExtra("cmd","start recording");
                startService(intent);
            }
        });

        mFloatLayout.setVisibility(View.INVISIBLE);

        mWindowManager.addView(mFloatLayout, mLayoutParams);

on the last line :( What am I doing wrong?

I tried to use also

mLayoutParams.type = LayoutParams.TYPE_PHONE;
mLayoutParams.type = LayoutParams.TYPE_SYSTEM_ALERT;

but with the same problem:

Process: com.poqdev.screenrec, PID: 20517
    java.lang.RuntimeException: Unable to create service com.poqdev.screenrec.service.RecordService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@90e7698 -- permission denied for window type 2038
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4204)
        at android.app.ActivityThread.access$1500(ActivityThread.java:237)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@90e7698 -- permission denied for window type 2038
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:1092)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:409)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109)
        at com.poqdev.screenrec.service.RecordService.createFloatView(RecordService.java:145)
        at com.poqdev.screenrec.service.RecordService.onCreate(RecordService.java:66)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4192)

What am I doing wrong?

Upvotes: 2

Views: 2306

Answers (1)

JacquesBauer
JacquesBauer

Reputation: 266

Hi this might be a bit late, but I had the same issue and I found that you have to give your app the "Display over other apps" permission inside of Settings. Once you set this to Allowed, then you should not see this error.

Upvotes: 2

Related Questions