1

'da görünmüyor. Android M için READ_PHONE_STATE iznini sormaya çalışıyorum ama Etkinlikler'de çalışıyor ve bunu parça halinde uyguladığımda diyalog kutusu göstermiyor. İşte kod. İşteAndroid M izin diyaloğu

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(); 
        } 

yöntem 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); 
     } 
    } 

Ve burada onRequestPermissionsResult üzerinde koddur.

Neler eksik? Şimdiden teşekkürler.

+0

Lütfen, cevabı burada tamamlayabilmeniz için bana yardımcı olabilir misiniz? –

cevap

1

requestReadPhoneStatePermission yöntemi yanlıştı.

DÜZENLEME ben upvoted olsun bu eski yazıyı gördüm ve doğru bir uygulama olmadığını fark etti.

İzin isteklerini yerine getirmenin doğru yolu budur.

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

}