Zeeshan Shabbir Zeeshan Shabbir - 2 months ago 8
Android Question

Android M permission dialogue not appearing in fragment

I am trying to ask for READ_PHONE_STATE permission for android M but it works in Activities and when i implement it in fragment it doesn't show dialogue box.
Here is the code.

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


Here is method
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);
}
}


And here is code on 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;
}
}


What i am missing? Thanks in advance.

Answer

You had the requestReadPhoneStatePermission method wrong.

EDIT

I saw this old post get upvoted and realized it was not a correct implementation.

This is the proper way to handle permission requests.

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

}
Comments