'checkSelfPermission' 的解决方法或弃用版本是什么?

What's the workaround or maybe deprecated version of 'checkSelfPermission'?

我正在尝试 运行 Android 示例 camera2basic。有一种调用 checkSelfPermission 的方法需要 API 级别 23,但我 运行 正在使用我的 phone,它只支持 API 级别 21。有什么解决方法或可能是 checkSelfPermission?

的弃用版本

PS: 正在检查相机权限

if (getActivity().checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
    requestCameraPermission();
    return;
}

确保您使用的是支持库的 v23,然后您可以使用 ContextCompat.checkSelfPermission,这是一种向后兼容旧 Android 版本的静态方法。

更多信息:ContextCompat

如 Google 的文档所述:

Revision 23 of the v4 and v13 support libraries provide several new methods for managing permissions. The support library methods work properly on any device that can use those libraries. Thus, if you use the support library methods, you do not need to check whether your app is running on a device with the M Developer Preview. If an app is installed on a device running the M Preview, the support library methods behave the same as their framework equivalents. If the device is running an earlier version of Android, the methods behave appropriately, as described below.

因此,对于您要尝试执行的操作,您应该使用:

ContextCompat.checkSelfPermission()

您需要的一切都应该在 Support library methods for handling permissions

中提供

编辑 2015 年 2 月 12 日: Google 移动了上面的 link。现在可在此处获得相同信息:Check for Permissions