无法获取安卓运行时权限结果



我想检查 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.
                }
            }
        }
    }

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

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

尝试此代码,并在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;
    }
}   

最新更新