0

UPDATE 2

I understand that SelfCheckPermission may help or the real answer but the problem is I can't find the right keyword.

If it is for camera, I will just need this code

if(CheckSelfPermission(Manifest.Permission.Camera)
                == Permission.Granted)

If it is other stuff like read contacts, I can use this code.

if(CheckSelfPermission(Manifest.Permission.ReadContacts)
                == Permission.Granted)

But I can't ask the Location if enabled in Application Settings using AccessCoarseLocation and AccessFineLocation since they are still both true even I disabled the Location on my App's Application Settings. Maybe I just need a right keyword for this and that's where I need your help.


UPDATE 1

Location Service is not what I meant here. Location Service, Coarse and Fine Location Permission can be enable or disable without relation to the Application Permission for Location on Application Settings. What I need is to check application settings from settings -> apps/applition -> your app -> location if location is disabled or not. Example app is this Waze with location disabled

enter image description here enter image description here


ORIGINAL POST

Everything I searched is to selfcheck the permission for coarse and fine location but it doesn't solve my issue since it is always granted even if the location from settings of app is disabled. I need to detect if the app settings is allowed for location or not.

What I have now is to redirect to app settings to allow needed permission. TIA.

enter image description here CTTO - Pic from Google

jace
  • 1,634
  • 3
  • 14
  • 41
  • Duplicate https://stackoverflow.com/questions/10311834/how-to-check-if-location-services-are-enabled –  Jan 30 '18 at 06:12
  • I'm sorry to say but this is not a duplicate of that. I'll just add to the question that this is not about location service rather location permission on application settings – jace Jan 30 '18 at 06:20
  • I am sorry, if it is not a duplicate, thanks –  Jan 30 '18 at 06:23
  • In the last pic, u select , Allow, then OS will be allowed to work –  Jan 30 '18 at 08:16
  • 1
    Yes it will after you allow it, what I need is a code to trigger that behaviour in my own application. If Permission is disabled from Application Settings then it will prompt that message where I need to allow that permission (in my case, it is Location Permission) – jace Jan 30 '18 at 08:20

2 Answers2

1

Using Android.Support.V4.App you can request perms (Manifest.Permission.LocationHardware) and review the results in OnRequestPermissionsResult to determine if the user has manually disabled the permission (or denied it when the system dialog was presented).

1) Implement ActivityCompat.IOnRequestPermissionsResultCallback

public override void OnRequestPermissionsResult (int requestCode, string[] permissions, Permission[] grantResults)
{
    if (requestCode == REQUEST_LOCATION) {
        Log.Info (TAG, "Received response for location permissions request.");

        if (!PermissionUtil.VerifyPermissions (grantResults)) {
            Log.Info (TAG, "Location permissions are NOT granted.");
            Snackbar.Make (layout, Resource.String.permissions_not_granted, Snackbar.LengthShort).Show ();
        }
    }
}

2) Perform a RequestPermissions on Manifest.Permission.LocationHardware:

ActivityCompat.RequestPermissions (this, new string[] {Manifest.Permission.LocationHardware}, REQUEST_LOCATION);

VerifyPermissions Helper method:

public static bool VerifyPermissions (Permission[] grantResults)
{
    // At least one result must be checked.
    if (grantResults.Length < 1)
        return false;

    // Verify that each required permission has been granted, otherwise return false.
    foreach (Permission result in grantResults) {
        if (result != Permission.Granted) {
            return false;
        }
    }
    return true;
}
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • Thanks Sushi ! I'll give it a try . – jace Jan 30 '18 at 07:27
  • I'm sorry but what is !PermissionUtil have? Is it your own method? – jace Jan 30 '18 at 07:31
  • @jace Sorry about that, cut and pasting out of my code, it is just a helper method that loops through all the perm results (you can request more then one check and thus get back more then one result). – SushiHangover Jan 30 '18 at 07:36
  • I'm using xamarin so I changed your code from public to protected but anyway how can I go to OnRequestPermissionsResult? It is not defined on the methods that I can override even though I already include the v4 on my using. After trigger of ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.LocationHardware }, 1); Nothing happens – jace Jan 30 '18 at 07:49
  • @jace ??? So am I... add the interface `ActivityCompat.IOnRequestPermissionsResultCallback` to your `Activity` and implement an `OnRequestPermissionsResult` override – SushiHangover Jan 30 '18 at 07:51
  • Sorry not to mention I also include the Interface. I triggered the OnRequestPermissionResult after I clean and rebuild, seems a VS issue. I can now go to the overridden method but I don't know why I ain't having result count nor permission count on string array even though I sent it in the signature. It shows zero so it goes back when grantResult.Length < 1 is called on VerifyPermissions. ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.LocationHardware }, 1); – jace Jan 30 '18 at 08:24
  • @jace the `grantResults` param in that case should contain one entry in the array, try deleting the app from the emulator/device and then perform a clean/rebuild. – SushiHangover Jan 30 '18 at 08:42
  • It still has nothing . Can I ask if your suggestion is the same as this? CheckSelfPermission(Manifest.Permission.LocationHardware) == Permission.Granted – jace Jan 30 '18 at 09:07
0

I hope this method should work properly:

protected boolean isLocationPermissionEnabled() {
    return (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
            == PackageManager.PERMISSION_GRANTED);
}
Rino
  • 1,215
  • 1
  • 16
  • 28
  • Please re-read the question. It is not about selfcheckpermission since it is always granted even if the application permission from settings is disabled – jace Jan 30 '18 at 06:21
  • The method I have shared will return true if the location permission is enabled in settings-->Apps --> your app --> permissions and false if location is disabled. I read the updated question also, still I feel you are looking for this. And I really don't understand what "It is not about selfcheckpermission since it is always granted even if the application permission from settings is disabled" means – Rino Jan 30 '18 at 06:56
  • I gave it a try but it is still true even if permission on settings is enabled. I can see it also just in your code since it is checking for COARSE permission just like checking a FINE location from manifest so it won't really solve the issue. COARSE and FINE location is already in manifest that's why it returns true in any way. – jace Jan 30 '18 at 07:12
  • Maybe what I lack here is the right keyword for location in settings. Coarse and Fine Location can't trigger it – jace Jan 30 '18 at 08:40
  • I can get camera permissions and etc but I can't get the location. If I can only get the right keyword maybe your answer will work. But now I can't use it with just a COARSE checking – jace Jan 30 '18 at 09:05
  • @jace did you get any solution for your problem – Rino Feb 02 '18 at 12:12
  • none so far Rino. Sorry for late reply, I had my weekend from work . You're answer is really a good one but I can't use coarse nor fine location to check if the application settings for location is still enabled or not unlike other settings like camera, etc – jace Feb 05 '18 at 02:13