Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying popup windows while running in the background?

I am trying to open an activity from a class that extends Service. I am performing this task when the app is not in foreground/not being used. I can see in the logs that my service class triggered start activity with Intent.FLAG_ACTIVITY_NEW_TASK flag. But the activity does not open. But when the service triggers the same activity when the app is in foreground/being used, the activity opens.

After few searches I found out that I need to manually give the permission "display popup windows while running in the background android" in the other permissions section in the app settings.

The display over other apps permission SYSTEM_ALERT_WINDOW only gives access to "Display popup window".

Other permissions

In the image above, SYSTEM_ALERT_WINDOW will only grant you access to "Display popup window" and this works fine if the app is in foreground/being used but the permission marked in red if granted will give you access to open any activity directly from a class extending Service.

How do I check or ask the user to give "Display popup windows while running in the background" permission? or Is this permission restricted?

I see apps like Whatsapp have this checked by default.

like image 374
Sidharth MA Avatar asked Jan 08 '20 12:01

Sidharth MA


People also ask

What is meaning of display popup window while running in the background?

Display pop-up windows while running in the background : this one means to allow the app to draw over the system ui while the app is running in the background like services and broadcast.

What is pop-up window Android?

android.widget.PopupWindow. This class represents a popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.

What is a pop-up window?

A pop-up window is a window that appears on top of a help topic. The pop-up window sizes automatically to fit the amount of text or the size of the image in it. Pop-up windows remain on the screen until users click anywhere inside or outside of them.


2 Answers

This is common problem in Xiaomi devices for developers,

the solution we have for the moment is to redirect users to other permissions screen and let the users give the permission manually :

if ("xiaomi" == Build.MANUFACTURER.toLowerCase(Locale.ROOT)) {
    val intent = Intent("miui.intent.action.APP_PERM_EDITOR")
    intent.setClassName("com.miui.securitycenter",
            "com.miui.permcenter.permissions.PermissionsEditorActivity")
    intent.putExtra("extra_pkgname", getPackageName())
    startActivity(intent)
}

You can use Settings.canDrawOverlays(this) to test only if the user has given the Display pop-up windows which means the app can draw over the system ui.

The other two permissions can not be verified if the user has given them or not :

  1. Display pop-up windows while running in the background : this one means to allow the app to draw over the system ui while the app is running in the background like services and broadcast
  2. Show on lock screen : this one gives the app the permission to run even when the screen is locked like social apps (whatsApp , Viber , Messnger ...)
like image 139
elhoucine ayoub Avatar answered Oct 18 '22 03:10

elhoucine ayoub


Since there is no official way to check the "Display pop-up windows while running in the background" permission, I used a little trick to check the permissions.

Check if the app can draw overlays like below and then if the device is xiaomi navigate the user to the specific settings screen like below.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                if ("xiaomi".equals(Build.MANUFACTURER.toLowerCase(Locale.ROOT))) {
                    final Intent intent =new Intent("miui.intent.action.APP_PERM_EDITOR");
                    intent.setClassName("com.miui.securitycenter",
                            "com.miui.permcenter.permissions.PermissionsEditorActivity");
                    intent.putExtra("extra_pkgname", getPackageName());
                    new AlertDialog.Builder(this)
                            .setTitle("Please Enable the additional permissions")
                            .setMessage("You will not receive notifications while the app is in background if you disable these permissions")
                            .setPositiveButton("Go to Settings", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    startActivity(intent);
                                }
                            })
                            .setIcon(android.R.drawable.ic_dialog_info)
                            .setCancelable(false)
                            .show();
                }else {
                    Intent overlaySettings = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
                    startActivityForResult(overlaySettings, OVERLAY_REQUEST_CODE);
                }
            }
        }
like image 33
Kasun Thilina Avatar answered Oct 18 '22 03:10

Kasun Thilina