Application Not Asking to get Permissions

2019-08-27 04:14发布

问题:

I have this code to get the following permissions (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();
}

I know I don't have these permissions because I get an error like so:

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

Also: It is not requesting my permission to get access to READ_CONTACTS and WRITE_CONTACTS.

Whats the issue?

回答1:

You are using && instead of ||. As it stands, you will not ask for any runtime permissions if you hold just one of the three.

Also, you are calling requestPermissions() three times. Call it once, with whichever permissions you need. new String[] creates a string array, and it can hold all three of your desired permission names.



回答2:

If you have 10 permissions, will you extend your code by 20 more lines. Noooo!

You have 2 ways

  1. Follow correct way to ask permission. I am posting my code snippet, copy this code in your Base Activity class.
  2. Or you use RxPermissions. If you are new to Android.

Here is my code snippet.

String[] permissions = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS};

        getBulkPermissions(permissions, new RequestPermissionAction() {
            @Override
            public void permissionDenied() {
                // TODO: 5/27/2018 handle permission deny 
            }

            @Override
            public void permissionGranted() {
                // TODO: 5/27/2018 you code do further operations 
            }
        });

Can it be more simpler?

Now you will put this code Base Activity class and use anywhere you need. Or put in your current class if you don't need it at any other place.

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();
}