Kevin Farel
Kevin Farel

Reputation: 157

Permission to display pop-up windows while running in the background

I have a project that needs to display pop-up windows while running in the background permission. How to ask the user permission. I already tried the Setting.canDrawOverlay() but it doesn't work for the app that killed.

this is my app permission in setting

I only can get the green one permission but I need the red one.

Thanks.

Upvotes: 11

Views: 7438

Answers (2)

This is common problem with xiaomi devices

canDrawOverlay permission seems not to be enough for xiaomi

try this:

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);
            }

Upvotes: 0

Letaris
Letaris

Reputation: 101

This permission has to be given manually, but after searching for some time I came up with an idea that can be used to open the correct permission screen, so the user can activate the permission.

    fun isMiUi(): Boolean {
        return getSystemProperty("ro.miui.ui.version.name")?.isNotBlank() == true
    }

    fun isMiuiWithApi28OrMore(): Boolean {
        return isMiUi() && Build.VERSION.SDK_INT >= 28
    }

    fun goToXiaomiPermissions(context: Context) {
        val intent = Intent("miui.intent.action.APP_PERM_EDITOR")
        intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity")
        intent.putExtra("extra_pkgname", context.packageName)
        context.startActivity(intent)
    }

    private fun getSystemProperty(propName: String): String? {
        val line: String
        var input: BufferedReader? = null
        try {
            val p = Runtime.getRuntime().exec("getprop $propName")
            input = BufferedReader(InputStreamReader(p.inputStream), 1024)
            line = input.readLine()
            input.close()
        } catch (ex: IOException) {
            return null
        } finally {
            if (input != null) {
                try {
                    input.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
        return line
    }

In my project I verify if isMiuiWithApi28OrMore() and if it is then I can prepare an additional step telling the user that the app needs the permission and sends him to the permission screen.

I hope it helps.

Upvotes: 6

Related Questions