How to check for Mock Location in Android Marshmallow?

Bharath picture Bharath · Sep 19, 2015 · Viewed 21.8k times · Source

How to detect if the location is triggered with Mock Location in Android Marshmallow?

Previously, I used Settings.Secure.ALLOW_MOCK_LOCATION

But, in API level 23, this has been deprecated.

I want to check if the trigger is with Fake location or original GPS ?

Now, what is the alternative to check if Mock Location is enabled or not Android Marshmallow?

Answer

Nikola Despotoski picture Nikola Despotoski · Sep 27, 2015

If you are using FusedLocationProviderApi and have set mock location using setMockLocation(mGoogleApiClient, fakeLocation); in the callback/pendingIntentCallback the returned Location object contains KEY_MOCK_LOCATION extra, a boolean that indicates that received object is mock.

@Override

public void onLocationChanged (Location location){    
   Bundle extras = location.getExtras();
   boolean isMockLocation = extras != null && extras.getBoolean(FusedLocationProviderApi.KEY_MOCK_LOCATION, false);

}