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

Kev*_*rel 9 android android-permissions

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

这是我在设置中的应用权限

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

谢谢。

小智 5

此权限必须手动授予,但经过搜索一段时间后,我想出了一个想法,可用于打开正确的权限屏幕,以便用户可以激活该权限。

    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
    }
Run Code Online (Sandbox Code Playgroud)

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

我希望它有帮助。