Skip to content

Instantly share code, notes, and snippets.

@piyushgupta27
Last active September 27, 2022 07:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save piyushgupta27/bfedc6c9c02373cbba9fcd0bb5ec3579 to your computer and use it in GitHub Desktop.
Save piyushgupta27/bfedc6c9c02373cbba9fcd0bb5ec3579 to your computer and use it in GitHub Desktop.
Methods to detect Mock Locations in Android
/**
* For Build.VERSION.SDK_INT < 18 i.e. JELLY_BEAN_MR2
* Check if MockLocation setting is enabled or not
*
* @param context Pass Context object as parameter
* @return Returns a boolean indicating if MockLocation is enabled
*/
public static Boolean isMockLocationEnabled(Context context) {
return !Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0");
}
/**
* For Build.VERSION.SDK_INT >= 18 i.e. JELLY_BEAN_MR2
* Check if the location recorded is a mocked location or not
*
* @param location Pass Location object received from the OS's onLocationChanged() callback
* @return Returns a boolean indicating if the received location is mocked
*/
public static boolean isMockLocation(Location location) {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && location != null && location.isFromMockProvider();
}
@ky1290
Copy link

ky1290 commented Oct 8, 2020

Screenshot_20201006-154316_Chrome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment