Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do i need android.permission.SYSTEM_ALERT_WINDOW for WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY?

I have seen: WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY always in combination with <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> in AndroidManifest.xml. For example here.

Consider this code:

    windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
    surfaceView = new SurfaceView(this);
    WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
            100, 100,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT
    );
    layoutParams.gravity = Gravity.START | Gravity.TOP;
    windowManager.addView(surfaceView, layoutParams);

Do i need for this code SYSTEM_ALERT_WINDOW permissions?

From doc about SYSTEM_ALERT_WINDOW:

Allows an app to create windows using the type TYPE_SYSTEM_ALERT, shown on top of all other apps. Very few apps should use this permission; these windows are intended for system-level interaction with the user.

It's about TYPE_SYSTEM_ALERT not about TYPE_SYSTEM_OVERLAY. Hm?

UPDATE:
line: windowManager.addView(surfaceView, layoutParams);

java.lang.RuntimeException: Unable to create service: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@10dca8ac -- permission denied for this window type

So probably <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> is necessary.

like image 297
t0m Avatar asked Feb 21 '17 10:02

t0m


1 Answers

Change
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY to WindowManager.LayoutParams.TYPE_TOAST.
android.permission.SYSTEM_ALERT_WINDOW removed from Apps settings and it works.

Inspired: here

WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
            100, 100,
            WindowManager.LayoutParams.TYPE_TOAST,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
);

It seems that this solution do not need android:name="android.permission.SYSTEM_ALERT_WINDOW"

UPDATE
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

like image 112
t0m Avatar answered Oct 17 '22 16:10

t0m