我对新的权限检查系统有疑问,因为我使用的是android 5.0.3到7.X版本(API-23和更高版本),我不明白检查权限系统是如何工作的。我知道,manager.openCamera需要一个权限检查方法才能正常工作。首先,我在一个类(而不是活动)中工作,在"this“中我需要获取上下文,但我不能使用get context,因为它适用于api 24……那么,对于哪件事,我需要替换这个" this“,然后我需要做一些其他的事情?谢谢你所做的一切
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
manager.openCamera(cameraId, mStateCallback, backgroundHandler);发布于 2016-10-22 18:21:01
您可以在该类的构造函数中传递上下文,将其存储在成员变量中,然后将"this“替换为该成员变量(或者您可以将其作为参数传递给运行代码的方法)。
但是为了请求您需要的权限和activity,您可以传递activity,而不是“只是”传递上下文,这样您就可以使用activity来调用checkSelfPermission和requestPermissions。
现在的问题是:您将如何处理用户的响应?您应该在同一个活动中实现方法onRequestPermissionsResult。如果任何活动应该能够处理此问题,请创建一个BaseActivity类(从activity继承)并在BaseActivity中覆盖该方法,其他所有活动都应从该BaseActivity继承。
https://stackoverflow.com/questions/40190780
复制相似问题