4

I'm developing an app where a user will not be able to use it if mock location setting is enabled using this piece of code

if (Settings.Secure.getString(context.getContentResolver(),
            Settings.Secure.ALLOW_MOCK_LOCATION).equals("0"))
        return false;
    else
        return true;

well it was working fine in most of my test devices from KitKat to Marshmallow systems, until I tried my app on this single device with Marshmallow OS, the mock setting is clearly OFF, but that code above keeps telling me that the mock setting is ON, is this a bug? or am i missing something here?

ankuranurag2
  • 2,300
  • 15
  • 30
Jeff
  • 519
  • 1
  • 10
  • 18

2 Answers2

8

Checking out this answer from here.

boolean isMock = false;
if (android.os.Build.VERSION.SDK_INT >= 18) {
    isMock = location.isFromMockProvider();
} else {
    isMock = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0");
}

This might help you

Community
  • 1
  • 1
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
  • thanks for the response, I've been looking into it, im just confused that this simple code works on 2 samsung Marshmallow systems but it doesnt on another 1 – Jeff May 06 '16 at 05:24
  • I dont understand why do i need to add it in the manifest as I dont intend to use mock locations in my app, I just need to check if the mock setting is on or off and it does not work in 1 critical device. Well .. I did try to add it but it tells me that I should only put it in debug manifest – Jeff May 06 '16 at 05:33
  • @Jeff Sorry my bad – Nongthonbam Tonthoi May 06 '16 at 05:37
  • Perhaps this could be helpful http://stackoverflow.com/questions/32668494/how-to-check-for-mock-location-in-android-marshmallow – Nongthonbam Tonthoi May 06 '16 at 05:43
1

According to the android developer reference: https://developer.android.com/reference/android/provider/Settings.Secure.html ALLOW_MOCK_LOCATION is now deprecated in SDK 23.

The default value you will get will always be 1 in marshmallow.

Marlon
  • 1,839
  • 2
  • 19
  • 42