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

问题描述 投票:6回答:1

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

this is my app permission in setting

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

谢谢。

android android-permissions
1个回答
0
投票

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

    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(),如果可以,那么我可以准备一个额外的步骤,告诉用户该应用程序需要权限,并将其发送到权限屏幕。

希望对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.