5

enter image description here

and here is another example:

enter image description here

from the screenshot above we see user is able to disable picture in picture mode. you can find it in the "special app access" screen on the emulator api 27 . How can i detect if user has disabled this feature ?

i tried checking the following but it does not work:

packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)

compiler states that AppOpsManager cannot be found. any ideas ?

4

3 回答 3

9

就像 AlexTa 说的。但我想实际编写代码以节省一些时间:

private fun hasPermission(): Boolean {
    val appOps = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
    } else {
        return false
    }
    return appOps.checkOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, android.os.Process.myUid(), packageName) == AppOpsManager.MODE_ALLOWED
}
于 2018-10-02T02:56:25.313 回答
6

试试看AppOpsManager.checkOp (String op, int uid, String packageName)OPSTR_PICTURE_IN_PICTURE操作在op哪里。如果支持画中画操作,该方法应返回MODE_ALLOWED常量。

有关更多信息,请查看链接。

于 2018-10-01T15:38:26.377 回答
4

我可能会迟到,但这是答案

private fun hasPermission(): Boolean {
    val appOps = getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager?
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            appOps?.unsafeCheckOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, android.os.Process.myUid(), packageName) == AppOpsManager.MODE_ALLOWED
        } else {
            appOps?.checkOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE, android.os.Process.myUid(), packageName) == AppOpsManager.MODE_ALLOWED
        }
    } else {
        false
    }
}
于 2020-07-13T12:44:55.987 回答