简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM