在我的应用中,我正在访问手机通讯录。所以我在Android marshmallow设备上处理相同的权限。权限对话框按预期显示。问题:每当我点击对话框的拒绝按钮时,对话框都不会被解除。 这是我的代码:
private static final int REQUEST_READ_CONTACTS = 0;
String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS,,Manifest.permission.WRITE_CONTACTS};
活动onCreate:
if(!requestPermissionForContact(this, PERMISSIONS)){
ActivityCompat.requestPermissions(CurrentActivity.this, PERMISSIONS, REQUEST_READ_CONTACTS);
}
else{
getContactList(); //a method to accesses the contacts
}
requestPermissionForContact方法定义:
public boolean requestPermissionForContact(Context context,String... permissions){
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(EmergencyContact.this,
Manifest.permission.READ_CONTACTS)){
//Toast.makeText(this,"You gotta allow permission", Toast.LENGTH_LONG).show();
}
return false;
}
}
}
return true;
}
在onRequestPermissionResult内部:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//mPresenter.userRespondedForPermissionDialog(requestCode,REQUEST_READ_CONTACTS,grantResults);
switch (requestCode){
case REQUEST_READ_CONTACTS : {
if (grantResults.length >= 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted , Access contacts here or do whatever you need.
getContactList(); //Accessing the contacts
}
else{
Toast.makeText(this,"Oops you just denied the permission", Toast.LENGTH_LONG).show();
}
return;
}
}
}
问题:我面临的问题是点击拒绝,权限对话框不会被解雇。请帮忙。非常感谢提前。
用于测试的设备:使用Android Marshmallow 6.0.1的OnePlus One
答案 0 :(得分:1)
我遇到的问题是点击“拒绝”按钮,同样的活动再次加载,导致权限对话再次出现。