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.