0

Ich möchte prüfen, SMS Leseberechtigung gewährt oder nicht in API 23+. Also implementierte ich es wie folgt;Ich bin nicht in der Lage, Android Runtime Erlaubnis Erlaubnis Ergebnis

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

Bearbeitungsberechtigung bzw. wie folgt;

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

von Standard-SMS ist erlaubt, so checkSelfPermission() reterns Null, aber wenn ich die Erlaubnis manuell verweigern aus Gerät dann auch checkSelfPermission Einstellung() gibt den Wert Null.

Ich verstehe nicht, wie SMS-Berechtigung überprüft wird verweigert oder nicht. Bitte gib mir einige Lösungen.

+0

hat Ihre App targetSdkVersion 23 oder oben erwähnt? – Nilabja

+0

ist die Berechtigung in 'AndroidManifest.xml' Datei und in diesem Code gleich? weil es viele Berechtigungen im Zusammenhang mit SMS gibt – arjun

+0

löschen Sie Ihre App-Daten ... von Settings – rafsanahmad007

Antwort

0

dieses Codes versuchen und bieten auch Berechtigungen 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; 
    } 
} 
+0

Danke @Daryl Es funktioniert für mich – Priyanka

+0

Willkommen @Priyanka –