我想檢查SMS讀取權限是否授予API 23+。所以我執行如下;無法獲取Android運行時權限結果
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()reterns零,但是當我手動拒絕來自設備的設置,然後也checkSelfPermission()允許返回零值。
我不明白如何檢查SMS權限是否被拒絕。請給我提供一些解決方案。
沒有您的應用程序已經targetSdkVersion 23或以上提到的? – Nilabja
是'AndroidManifest.xml'文件中的權限,並且在這個代碼中是相同的?因爲有許多相關的權限短信 – arjun
清除您的應用程序數據... from settigns – rafsanahmad007