1

I am trying to set up location permission in android when I install the app in device automatically device assign the location permission to the app(using ManifestFile). if I manually disable the location from permission & run the app again It doesn't show me any popup (which I programmed to ask).

private const int LOCATION_GROUP_PERMISSION_REQUEST = 1;
if ((int)Build.VERSION.SdkInt > 22) {
            if (ContextCompat.CheckSelfPermission (this, Android.Manifest.Permission_group.Location) != Android.Content.PM.Permission.Granted) {
                Toast.MakeText (this, "Something Really wrong", ToastLength.Short).Show ();
                var data = ActivityCompat.ShouldShowRequestPermissionRationale (this, Manifest.Permission_group.Location);
                if (!data) {
                    AlertDialog.Builder builder;
                    builder = new AlertDialog.Builder (this);
                    builder.SetTitle ("Location Permission is Disabled");
                    builder.SetMessage ("Location permission is needed ");
                    builder.SetCancelable (false);
                    builder.SetPositiveButton ("Enable", delegate {
                        ActivityCompat.RequestPermissions (this, new String [] { Manifest.Permission_group.Location },
                                                       LOCATION_GROUP_PERMISSION_REQUEST);
                    });
                    builder.Show ();
                } else {

                    ActivityCompat.RequestPermissions (this, new String [] { Manifest.Permission_group.Location },
                                           LOCATION_GROUP_PERMISSION_REQUEST);
                }

            } else {
                GoToActivity ();
            }
        }

the data variable always return false

        public override void OnRequestPermissionsResult (int requestCode, string [] permissions, Android.Content.PM.Permission [] grantResults)
    {

        if (requestCode == LOCATION_GROUP_PERMISSION_REQUEST) {

            for (int i = 0; i < permissions.Length; i++) {
                if (grantResults [i] == Android.Content.PM.Permission.Granted) {
                    Toast.MakeText (this, "Param granted", ToastLength.Short).Show ();
                } else if (grantResults [i] == Android.Content.PM.Permission.Denied) {
                    Toast.MakeText (this, "param Denied", ToastLength.Short).Show ();
                }
            }
        } else {
            base.OnRequestPermissionsResult (requestCode, permissions, grantResults);
        }

    }

// permissions length Zero

imrohit
  • 149
  • 3
  • 10

2 Answers2

2

Runtime permission is only from Api level 23 and above I know that this answer is not perfect answer for your question, i answered in native so at least it help you to convert it to xamarin.

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED
                        || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {
                    // permissions have not been granted.
                        requestPermissions();

                } 
            }
      }


 private void requestPermissions() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_COARSE_LOCATION)
                || ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            // Provide an additional rationale to the user if the permission was not granted
            // and the user would benefit from additional context for the use of the permission.
            // For example, if the request has been denied previously.
            // this is my custom dialog change as per ur requirement to notify user
            showPermissionRationaleDialog("Locations permissions are needed to demonstrate access.", PERMISSIONS_LOCATION, false);
          //when user click ok in dialog you have to call requestForPermission method
        } else {
            // permissions have not been granted yet. Request them directly.
            requestForPermission(PERMISSIONS_LOCATION);
        }
    }

//request generating

private void requestForPermission(final String[] permissions) {
        ActivityCompat.requestPermissions(NwSelectionActivity.this, permissions, REQUEST_CODE);
    }

//result handling

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {

        if (requestCode == REQUEST_CODE) {

            if (PermissionUtil.verifyPermissions(grantResults)) {
                // All required permissions have been granted,

            } else {
                boolean showRationale = ActivityCompat.shouldShowRequestPermissionRationale(this,
                        permissions[0]);
                Log.i(TAG, "Locations permissions were NOT granted.");
                if (!showRationale) {
   //here you show dialog to user to manually enable location permission in setting
                    showPermissionRationaleDialog("Allow App to access your locations. Tap Setting > Permissions, and turn Location on.", PERMISSIONS_LOCATION, true);
                }
            }


        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
Nas
  • 2,158
  • 1
  • 21
  • 38
  • I did everything same & ACCESS_FINE_LOCATION & ACCESS_FINE_LOCATION comes true even i disable the button from permissions. When i checking Permission_group.Location then it coming false. I used the above code & onRequestPermissionsResult my permissions coming blank. – imrohit Dec 08 '16 at 09:21
  • again you should check Rationale status in else condition to further check the permission disabled manually here //if (grantResults [i] == Android.Content.PM.Permission.Denied) – Nas Dec 08 '16 at 10:05
0

Check if the permission was denied or granted in onRequestPermissionsResult.

If the permission was denied previously, this time there will be a Never ask again checkbox in the permission dialog.

Call shouldShowRequestPermissionRationale to see if the user checked Never ask again. shouldShowRequestPermissionRationale method returns false only if the user selected Never ask again or device policy prohibits the app from having that permission:

see https://stackoverflow.com/a/34612503/4623782 for more information

Community
  • 1
  • 1
Raymond
  • 2,276
  • 2
  • 20
  • 34
  • I am compiling on API 25 and it never asked me before to enable that, I never checked that, It checked that Permission_group.Location is disabled & then it show the popup but when I click enable nothing happens – imrohit Dec 08 '16 at 09:24