1

J'essaie de demander l'autorisation READ_PHONE_STATE pour android M mais cela fonctionne dans les activités et quand je l'implémente dans le fragment il ne montre pas la boîte de dialogue. Voici le code.Android M permission dialogue n'apparaît pas dans le fragment

if (preference.getToken() == null) { 
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { 
         requestReadPhoneStatePermission(); 
        } else { 
         TelephonyManager tm = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE); 
         HashMap<String, String> params = new HashMap<String, String>(); 
         params.put("appCode", Constants.TRACKING_ID); 
         params.put("phone", tm.getDeviceId()); 
         DeviceUserService.getDeviceUser(params, getContext()); 
         bookmark(); 
        } 

Voici la méthode requestReadPhoneStatePermission

public void requestReadPhoneStatePermission() { 
     if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.READ_PHONE_STATE)) { 

     } else { 
      ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_PHONE_STATE}, READ_PHONE_STATE); 
     } 
    } 

Et voici le code sur onRequestPermissionsResult.

@Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     switch (requestCode) { 
      case READ_PHONE_STATE: 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        Toast.makeText(getContext(), "granted", Toast.LENGTH_LONG).show(); 

       } else { 
        Toast.makeText(getContext(), "permission not granted", Toast.LENGTH_LONG).show(); 
       } 
       return; 
     } 
    } 

Qu'est-ce qui me manque? Merci d'avance.

+0

Veuillez, Pouvez-vous poster la complète la réponse ici, donc il peut me aider? –

Répondre

1

La méthode requestReadPhoneStatePermission était incorrecte.

EDIT

J'ai vu ce vieux poste s'upvoted et a réalisé qu'il n'a pas été une bonne mise en œuvre.

C'est la bonne façon de gérer les demandes d'autorisation.

public void requestReadPhoneStatePermission() { 
    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_PHONE_STATE}, READ_PHONE_STATE); 
} 



@Override 
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if(requestCode==READ_PHONE_STATE){ 
     if(grantResults[0]==PackageManager.PERMISSION_GRANTED){ 
      //do your thing 
     } 
     else{ 
      if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_PHONE_STATE)){ 
       //user denied the permission but did not check the "never show again" option. 
       //You can ask for the permission again or show a dialog explaining 
       //why you need the permission with a button that requests the permission again on click. 
      } 
      else{ 
       //user denied the permission and checked the "never show again" option. 
       //Here you can show a dialog explaining the situation and that the user has 
       //to go to the app settings and allow the permission otherwise yor feature 
       //will not be available. 
      } 
     } 
    } 

}