Search code examples
androidandroid-permissions

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.


Solution

  • 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.