1

I integrated Android Runtime Permission for READ PHONE STATE for Marshmallow devices. This implementation is working fine and the popup is showing in my application with allow/deny option.

I can click the allow/ deny button for normal marshmallow devices. But in the case of updated android devices(From Lollipop to Marshmallow), the allow button click is not working initial time. This issue is tested and reproduced in Nexus 5 and Nexus 7. Is anything we need to create additional for updated OS? Or is it a Marshmallow issue?

Please check the complete code:

    private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 101;

if (Build.VERSION.SDK_INT >= 23) {
   if(mActivity.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) !=PackageManager.PERMISSION_GRANTED) {
                                if (this.shouldShowRequestPermissionRationale(
                                        Manifest.permission.READ_PHONE_STATE)) {
                                    showExplanationDialog(mActivity, getString(R.string.dialog_message_phone_state));
                                } else {
                                    this.requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
                                            MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
                                }
                            } else {
                                handleLoginAPI();
                            }
                        } else {
                            handleLoginAPI();
                        }

     private void handleLoginAPI() {
      if (super.isNetworkConnectionAvailable(mActivity)) {
        // Api Call from here.. 
      }else{
        // No Nw Connection.
      }

    }

    @Override
        public void onRequestPermissionsResult(int requestCode,
                                               String permissions[], int[] grantResults) {
            switch (requestCode) {
                case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
                    // If request is cancelled, the result arrays are empty.
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        handleLoginAPI();
                    } else if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                        if (this.shouldShowRequestPermissionRationale(
                                Manifest.permission.READ_PHONE_STATE)) {
                            showExplanationDialog(mActivity, getString(R.string.dialog_message_phone_state));
                        }
                    }
                    return;
                }
            }
        }
Nithinjith
  • 1,775
  • 18
  • 40

3 Answers3

1

Don't call shouldShowRequestPermissionRationale() before requesting the permission. That call is intended to be made after the user has rejected the request, usually in the onRequestPermissionsResult() method. If your app needs to explain why it needs a permission before it asks for it, the app's internal logic needs to decide that.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • I am following the android developer console sample code. https://developer.android.com/training/permissions/requesting.html. You have any updated code. Please share if possible. – Nithinjith Jul 04 '16 at 13:24
  • Was the app previously installed (before the upgrade)? If so, the permission will have been automatically granted. If not, then it's supposed to be like a (new) Marshmallow device. However, if the permission has never been requested (and rejected) then `shouldShowRequestPermissionRationale()` will most likely return `false`. I've had the most reliable results internally tracking whether the first use has been "explained" and only rely on "show rationale" if the permission is rejected by the user. It will only return `true` if rejected and the user did not choose to supress future requests. – Larry Schiefer Jul 04 '16 at 13:48
  • If interested, there is an open source helper library which makes this easier to deal with: https://github.com/hiqes/andele – Larry Schiefer Jul 04 '16 at 13:49
  • Thanks for the comment. My application installed after the OS update. I will try with this library and update you soon. – Nithinjith Jul 05 '16 at 02:10
  • 1
    This is an Android OS update issue. https://code.google.com/p/android/issues/detail?id=213120. Please check the link. This issue reported in nexus device. One more thing I notice that the issue is happening in all Android native applications (Map, Gmail etc.) – Nithinjith Jul 07 '16 at 08:29
1
 private void setCheck()
    {
        int hasWriteContactsPermission = 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            hasWriteContactsPermission = checkSelfPermission(android.Manifest.permission.READ_PHONE_STATE);
            if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
                if (!shouldShowRequestPermissionRationale(android.Manifest.permission.READ_PHONE_STATE)) {
                    showLocationRationleDalog();
                    return;
                }
                return;
            } else {
                handleLoginAPI();
            }
        } else {
            handleLoginAPI();
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length == 1
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    handleLoginAPI();
                } else {
                    Log.i("Permission denied");
                }
                return;
            }
        }
    }
Anantha Babu
  • 216
  • 2
  • 14
0

This is an Android OS update issue. The issue is reported in Nexus device. https://code.google.com/p/android/issues/detail?id=213120. The issue can be reproducible in other android native application as like Google map, Gmail etc.

Nithinjith
  • 1,775
  • 18
  • 40