I am trying to find a way of reading the new settings for mock locations in Android M.
Before Android M, the mock location setting was a toggle option called "use mock locations", and it was readable with this setting: Settings.Secure.ALLOW_MOCK_LOCATION
With Android M, the mock location setting is an app to select for mock locations, so it is a multi-elements list, which selected element can be "none".
That thread has a solution for determining the source of the location update when listening for locations, but unfortunately I would like to read the setting itself: How to check for Mock Location in Android Marshmallow?
I would like to read whether there is a selected app for mock locations or not, and ideally the selected app as well. Any idea about how to do that are welcome!
After you download the app, go to Settings > Developer options and tap on Mock location app. Tap on it and it'll show all the available mock location apps installed on your device. Choose one and you're good to go. You can use the apps to select a location of your choice.
On Android 18 (JellyBean MR2) and above mock locations are detected using Location. isFromMockProvider() for each location. The app can detect that the location came from a mock provider when the API returns true.
mock locations don't work on some devices no matter what app you use. and just to be sure check if you have mock locations on in developer options. In Android 6 there is no Allow Mock Locations option anymore; there is an option to choose the app which will fake the position instead.
Ok digging through the android developer site this is what I have come up with. You have to use the AppOpsManager. checkOp("android:mock_location", "yourUID", "com.your.package.name")
I think you can check if a different app is enabled by changing the package name.
public boolean isMockLocationEnabled(){
boolean isMockLocation = false;
try {
//if marshmallow
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
AppOpsManager opsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
isMockLocation = (opsManager.checkOp(AppOpsManager.OPSTR_MOCK_LOCATION, android.os.Process.myUid(), BuildConfig.APPLICATION_ID)== AppOpsManager.MODE_ALLOWED);
} else {
// in marshmallow this will always return true
isMockLocation = !android.provider.Settings.Secure.getString(mContext.getContentResolver(), "mock_location").equals("0");
}
} catch (Exception e) {
return isMockLocation;
}
return isMockLocation;
}
For those attempting to do this using ADB, here are the commands for getting and setting the mock app:
Allowing app for mock locaiton
adb shell appops set <PACKAGE> android:mock_location allow
Removing app for mock location
adb shell appops set <PACKAGE> android:mock_location deny
Checking if app is set for mock location
adb shell appops get <PACKAGE> android:mock_location
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With