0

As per mentioned in https://stackoverflow.com/a/22442702/3286489, the below code would require

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

WindowManager.LayoutParams p = new WindowManager.LayoutParams(
    // Shrink the window to wrap the content rather than filling the screen 
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.WRAP_CONTENT,
    // Display it on top of other application windows, but only for the current user
    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
    // Don't let it grab the input focus
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
    // Make the underlying application window visible through any transparent parts
    PixelFormat.TRANSLUCENT);

// Define the position of the window within the screen
p.gravity = Gravity.TOP | Gravity.RIGHT;
p.x = 0;
p.y = 100;

WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
windowManager.addView(myView, p);

However, when checking through the source code of WindowManager.java, it seems to me that only if we use TYPE_APPLICATION_OVERLAY, then the permission is required.

        /**
         * Window type: Application overlay windows are displayed above all activity windows
         * (types between {@link #FIRST_APPLICATION_WINDOW} and {@link #LAST_APPLICATION_WINDOW})
         * but below critical system windows like the status bar or IME.
         * <p>
         * The system may change the position, size, or visibility of these windows at anytime
         * to reduce visual clutter to the user and also manage resources.
         * <p>
         * Requires {@link android.Manifest.permission#SYSTEM_ALERT_WINDOW} permission.
         * <p>
         * The system will adjust the importance of processes with this window type to reduce the
         * chance of the low-memory-killer killing them.
         * <p>
         * In multi-user systems shows only on the owning user's screen.
         */
        public static final int TYPE_APPLICATION_OVERLAY = FIRST_SYSTEM_WINDOW + 38;

The reason I ask is, I'm thinking of using windowManager.addView(myView, p); within my App only (to draw something over my app), and not on other app, or run in background service. I'm not using TYPE_APPLICATION_OVERLAY flag. I believe I don't need the request permission of <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

But I want to confirm, since the stackoverflow link I provide above seems to alluded that it need that permission.

Elye
  • 53,639
  • 54
  • 212
  • 474

1 Answers1

0

When we use TYPE_APPLICATION_OVERLAY, which could only be used on Service, we would require the <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> permission.

As TYPE_APPLICATION_OVERLAY is introduced in Android O, the earlier version like TYPE_SYSTEM_ALERT and TYPE_PHONE etc (that are deprecated and needing TYPE_APPLICATION_OVERLAY) needs the permission as well.

If one is using TYPE_APPLICATION_PANEL, then the permission is not required.

More detail is provided in the below link https://levelup.gitconnected.com/add-view-outside-activity-through-windowmanager-1a70590bad40

Elye
  • 53,639
  • 54
  • 212
  • 474