[JAVA] Display View on other apps on Android (Summary of support methods by API version)

When I was writing Android code for the first time in a long time, I stumbled upon trying to overlay the view on other apps, so I made a note. Since only the main points are extracted, please refer to the link listed as a reference site for detailed code.

Android 4.0-5.0 (API level 14-22)

AndroidManifest.xml


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

Pass `` `WindowManager.LayoutParams.TYPE_SYSTEM_ALERT to the layer type when LayoutParams is generated (int _type``` variable in the constructor).

Reference site: Always display application information on the screen

Android 6.0-7.1.1 (API level 23-25)

In addition to support up to Android 5.0, the following support is required.

The API has been updated to require users to grant permissions individually for some permissions.

First, call `` `Settings # canDrawOverlays``` to check the permissions of the app.

If you do not have the authority, call the screen to change the overlay authority. For Intent, specify Settings.ACTION_MANAGE_OVERLAY_PERMISSION and the package name of your application. (Display to the user and ask them to allow permission setting at the start of use)

When the control returns to your application from the permission setting screen, check the permission again, and if the permission is enabled, add the view in the same way as up to Android 5.0.

Reference site: The handling of SYSTEM_ALERT_WINDOW permissions has changed in Marshmallow

Android 8.0 ~ (API level 26 ~)

In addition to the support up to Android 7.1.1, the following support is required.

The API has been updated and the TYPE_SYSTEM_ALERT layer is no longer available. Therefore, using the same layer type WindowManager.LayoutParams.TYPE_SYSTEM_ALERT will result in an error. (The following exception occurs. I was troubled because I could not find the information even if I searched with this exception)

java.lang.RuntimeException: Unable to start service com.example.app.TestService@57b8b88 with Intent { cmp=com.example.app/.TestService }: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@4938b2b -- permission denied for window type 2003



 To avoid this, pass `` `WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY``` to the layer type when creating LayoutParams when the API level is 26 or higher.

 Reference site: [The behavior of WindowManager changes on Android O](http://keithyokoma.hatenablog.com/entry/2017/03/22/190000)



Recommended Posts

Display View on other apps on Android (Summary of support methods by API version)
Summary of revisions (new era) support by Java version
[Android] Let's snoop on other people's apps
Browse an instance of Tab's View on Android
Display text character by character in Android Surface View
Summary of Java support 2018
About truncation by the number of bytes of String on Android