0

我想檢查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權限是否被拒絕。請給我提供一些解決方案。

+0

沒有您的應用程序已經targetSdkVersion 23或以上提到的? – Nilabja

+0

是'AndroidManifest.xml'文件中的權限,並且在這個代碼中是相同的?因爲有許多相關的權限短信 – arjun

+0

清除您的應用程序數據... from settigns – rafsanahmad007

回答

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

謝謝@Daryl它適用於我 – Priyanka

+0

歡迎@Priyanka –