Last active
September 27, 2022 07:16
-
-
Save piyushgupta27/bfedc6c9c02373cbba9fcd0bb5ec3579 to your computer and use it in GitHub Desktop.
Methods to detect Mock Locations in Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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
commented
Oct 8, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment