允许在后台 运行 时显示弹出窗口 windows

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

我有一个项目需要在后台显示 pop-up windows 而 运行 permission。如何询问用户permission。我已经尝试过 Setting.canDrawOverlay(),但它不适用于杀死的应用程序。

this is my app permission in setting

我只能得到绿色的许可,但我需要红色的。

谢谢。

这个权限需要手动给,但是经过一段时间的搜索,我想到了一个可以用来打开正确权限屏幕的想法,这样用户就可以激活权限了。

    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
    }

在我的项目中,我验证是否 isMiuiWithApi28OrMore() 如果是,那么我可以准备一个额外的步骤,告诉用户该应用程序需要权限并将他发送到权限屏幕。

希望对您有所帮助。

这是小米设备的常见问题

canDrawOverlay权限对小米来说似乎不够用

试试这个:

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