无法获得Android运行时权限结果

时间:2017-02-09 12:08:26

标签: android android-permissions permission-denied user-permissions

我想检查API 23+中是否已授予短信阅读权限。所以我实现了如下:

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

处理权限。如下;

@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.
                }
            }
        }
    }

默认情况下,授予SMS权限,因此checkSelfPermission()会重新为零,但是当我手动拒绝设备设置的权限时,checkSelfPermission()也返回零值。

我不明白如何检查短信权限是否被拒绝。请给我一些解决方案。

1 个答案:

答案 0 :(得分:0)

尝试此代码并在android清单中提供权限

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