ContextCompat.checkSelfPermission(上下文, Manifest.permission.CAMERA) 总是 return 授予
ContextCompat.checkSelfPermission(Context, Manifest.permission.CAMERA) alway return granted
我在我的应用程序中使用 Android API lvl 23。当我检查相机权限时,ContextCompat.checkSelfPermission
总是 return 0 (== PackageManager.PERMISSION_GRANTED
)
我设法将它从 ContextCompat
更改为 ActivityCompat
。
这是我的代码:
public static boolean verify(Activity activity, final String[] PERMISSIONS, final int PERMISSIONS_REQUEST_ID) {
if (underAPI23())
return true;
String[] denyPermission = new String[PERMISSIONS.length];
int denyPermissionLength = 0;
boolean shouldShowRequest = false;
for (int i = 0; i < PERMISSIONS.length; i++) {
int check = ContextCompat.checkSelfPermission(activity, PERMISSIONS[i]);
LogUtils.e(TAG, PERMISSIONS[i] + ": " + (check != PackageManager.PERMISSION_GRANTED));
// ===== ===== =====
// This always return true. :'(
// ===== ===== =====
if (check != PackageManager.PERMISSION_GRANTED) {
denyPermission[denyPermissionLength++] = PERMISSIONS[i];
if (shouldShowRequest == false) {
boolean should = ActivityCompat.shouldShowRequestPermissionRationale(activity, PERMISSIONS[i]);
if (should)
shouldShowRequest = true;
}
}
}
if (denyPermissionLength > 0) {
if (shouldShowRequest) {
ActivityCompat.requestPermissions(activity, denyPermission, PERMISSIONS_REQUEST_ID);
} else {
ActivityCompat.requestPermissions(activity, denyPermission, PERMISSIONS_REQUEST_ID);
}
return false;
} else {
return true;
}
}
我在 Gradle build
中的依赖项
dependencies {
//...
compile 'com.android.support:support-v4:23.2.0'
compile 'com.android.support:appcompat-v7:23.2.0'
//...
}
已更新:权限调用
if (PermissionGrant.verify(getActivity(), new String[]{Manifest.permission.CAMERA}, GRANT_TAKE_PHOTO)) {
// Do my jobs
}
您的许可应该是Manifest.permission.<Android permission>
您的 Android 目标版本是什么? shouldShowRequestPermissionRationale
always return false
这意味着 ContextCompat.checkSelfPermission(activity, permission)
always return false at Android API lvl under 23 too.
Document here。请关注:
Note: If the user turned down the permission request in the past and chose the Don't ask again option in the permission request system dialog, this method returns false. The method also returns false if a device policy prohibits the app from having that permission.
我在我的应用程序中使用 Android API lvl 23。当我检查相机权限时,ContextCompat.checkSelfPermission
总是 return 0 (== PackageManager.PERMISSION_GRANTED
)
我设法将它从 ContextCompat
更改为 ActivityCompat
。
这是我的代码:
public static boolean verify(Activity activity, final String[] PERMISSIONS, final int PERMISSIONS_REQUEST_ID) {
if (underAPI23())
return true;
String[] denyPermission = new String[PERMISSIONS.length];
int denyPermissionLength = 0;
boolean shouldShowRequest = false;
for (int i = 0; i < PERMISSIONS.length; i++) {
int check = ContextCompat.checkSelfPermission(activity, PERMISSIONS[i]);
LogUtils.e(TAG, PERMISSIONS[i] + ": " + (check != PackageManager.PERMISSION_GRANTED));
// ===== ===== =====
// This always return true. :'(
// ===== ===== =====
if (check != PackageManager.PERMISSION_GRANTED) {
denyPermission[denyPermissionLength++] = PERMISSIONS[i];
if (shouldShowRequest == false) {
boolean should = ActivityCompat.shouldShowRequestPermissionRationale(activity, PERMISSIONS[i]);
if (should)
shouldShowRequest = true;
}
}
}
if (denyPermissionLength > 0) {
if (shouldShowRequest) {
ActivityCompat.requestPermissions(activity, denyPermission, PERMISSIONS_REQUEST_ID);
} else {
ActivityCompat.requestPermissions(activity, denyPermission, PERMISSIONS_REQUEST_ID);
}
return false;
} else {
return true;
}
}
我在 Gradle build
中的依赖项dependencies {
//...
compile 'com.android.support:support-v4:23.2.0'
compile 'com.android.support:appcompat-v7:23.2.0'
//...
}
已更新:权限调用
if (PermissionGrant.verify(getActivity(), new String[]{Manifest.permission.CAMERA}, GRANT_TAKE_PHOTO)) {
// Do my jobs
}
您的许可应该是Manifest.permission.<Android permission>
您的 Android 目标版本是什么? shouldShowRequestPermissionRationale
always return false
这意味着 ContextCompat.checkSelfPermission(activity, permission)
always return false at Android API lvl under 23 too.
Document here。请关注:
Note: If the user turned down the permission request in the past and chose the Don't ask again option in the permission request system dialog, this method returns false. The method also returns false if a device policy prohibits the app from having that permission.