Not able to get Android run time permission result

2019-09-17 09:00发布

问题:

I want to check SMS reading permission is Granted or not in API 23+. So I implemented its as follows;

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED){
           ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, PERMISSIONS_REQUEST_READ_SMS);
       }

Handling permission resp. as follows;

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        if (requestCode == PERMISSIONS_REQUEST_READ_SMS) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //Doing task regarding SMS permission.
            }else if (grantResults[0] == PackageManager.PERMISSION_DENIED){
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_SMS)) {
                    //Show an explanation to the user *asynchronously*
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("This permission is important to Read SMS.")
                            .setTitle("Important permission required");
                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            ActivityCompat.requestPermissions(GenerateOTPActivity.this, new String[]{Manifest.permission.READ_SMS}, PERMISSIONS_REQUEST_READ_SMS);
                        }
                    });
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, PERMISSIONS_REQUEST_READ_SMS);
                }else{
                    //Never ask again and handle your app without permission.
                }
            }
        }
    }

By default SMS permission is Granted so checkSelfPermission() reterns zero but when I manually deny permission from device setting then also checkSelfPermission() returns zero value.

I dont understand how to check SMS permission is denied or not. Please provide me some solutions.

回答1:

try this codes and also provide permissions in android manifest

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        marshmallowPermission();                        //***** marshmaloow runtime permission*****//
    }

 public Boolean marshmallowPermission(){

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkPermission()) {
            Toast.makeText(this,"Permission already granted.", Toast.LENGTH_SHORT).show();
        } else {
            requestPermission();
        }
    }
    return true;
}

@TargetApi(Build.VERSION_CODES.M)
public boolean checkPermission(){
int sms = checkSelfPermission(android.Manifest.permission.READ_SMS);

    if (sms == PackageManager.PERMISSION_GRANTED ){
        return true;
    } else {
        return false;
    }
}

@TargetApi(Build.VERSION_CODES.M)
public void requestPermission(){

    if (shouldShowRequestPermissionRationale(android.Manifest.permission.READ_SMS)){

         if(shouldShowRequestPermissionRationale(android.Manifest.permission.READ_SMS)){
            Toast.makeText(this,"sms Permission must be needed which allows to access sms . Please allow sms in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
        }

        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", this.getPackageName(), null);
        intent.setData(uri);
        this.startActivity(intent);

    } else {

        requestPermissions(new String[]{android.Manifest.permission.READ_SMS},100);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 100:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this,"Permission Granted, Now you can access app without bug.",Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this,"Permission Denied, App maybe get crashed.", Toast.LENGTH_LONG).show();
            }
            break;
    }
}