我有这段代码来获取以下权限 (READ_PHONE_STATE,READ_CONTACTS,WRITE_CONTACTS) :
int permissionCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE);
int permissionCheck1 = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS);
int permissionCheck2 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS);
if (permissionCheck != PackageManager.PERMISSION_GRANTED && permissionCheck1 != PackageManager.PERMISSION_GRANTED && permissionCheck2 != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_PHONE_STATE}, 1);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 2);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CONTACTS}, 3);
} else {
TelephonyManager tMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
}
我知道我没有这些权限,因为我收到了这样的错误:
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{95e4cb4 30797:com.example.ortel.tagnet/u0a217} (pid=30797, uid=10217) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
另外:我未经许可访问READ_CONTACTS
和WRITE_CONTACTS
。
问题是什么?
答案 0 :(得分:2)
您使用&&
代替||
。按照目前的情况,如果您只持有其中一个,则不会要求任何运行时权限。
此外,您正在呼叫requestPermissions()
三次。使用您需要的任何权限调用一次。 new String[]
创建一个字符串数组,它可以保存所有三个所需的权限名称。
答案 1 :(得分:0)
如果您有10个权限,您是否会将代码扩展20行。不要!
你有两种方式
这是我的代码段。
RequestPermissionAction onPermissionCallBack;
private final static int REQUEST_BULK_PERMISSION = 3006;
private boolean checkBulkPermissions(String[] permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
for (String permission : permissions) {
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
} else {
return true;
}
}
public void getBulkPermissions(String[] permissions, RequestPermissionAction onPermissionCallBack) {
this.onPermissionCallBack = onPermissionCallBack;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!checkBulkPermissions(permissions)) {
requestPermissions(permissions, REQUEST_BULK_PERMISSION);
return;
}
}
if (onPermissionCallBack != null)
onPermissionCallBack.permissionGranted();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (onPermissionCallBack != null)
onPermissionCallBack.permissionGranted();
} else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
if (onPermissionCallBack != null)
onPermissionCallBack.permissionDenied();
}
}
public interface RequestPermissionAction {
void permissionDenied();
void permissionGranted();
}
可以更简单吗?
现在,您将把此代码放在Base Activity类中,并在您需要的任何地方使用。如果你不想在任何其他地方需要它,请加入你当前的课程。
<input type="file" multiple="multiple" name="images" accept="image" id="images" />