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

时间:2019-11-26 13:17:55

标签: android android-permissions

我有一个项目,需要在后台pop-up中运行时显示permission窗口。如何询问用户permission。我已经尝试过Setting.canDrawOverlay(),但对于终止运行的应用程序无效。

this is my app permission in setting

我只能获得绿色的权限,但我需要红色的权限。

谢谢。

1 个答案:

答案 0 :(得分: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
    }

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

我希望这会有所帮助。