Android: Mock Location reverts to original again in few seconds

I have an Android app in which I need a user to make fun of my current location. Below is the code that I use so that the user presses the green button to start mocking their location.

Below the code begins to falsify the location when you press the "green button".

greenButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); lm.addTestProvider(LocationManager.GPS_PROVIDER, "requiresNetwork" == "", "requiresSatellite" == "", "requiresCell" == "", "hasMonetaryCost" == "", "supportsAltitude" == "", "supportsSpeed" == "", "supportsBearing" == "", Criteria.POWER_LOW, Criteria.ACCURACY_FINE); Location newLocation = new Location(LocationManager.GPS_PROVIDER); newLocation.setLatitude(fakeLocation.getLatitude()); newLocation.setLongitude(fakeLocation.getLongitude()); newLocation.setAccuracy(fakeLocation.getAccuracy()); newLocation.setTime(System.currentTimeMillis()); newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()); lm.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true); lm.setTestProviderStatus(LocationManager.GPS_PROVIDER, LocationProvider.AVAILABLE, null, System.currentTimeMillis()); lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, newLocation); } }); 

When I put a marker on the map and press the green button. The place mockingly begins. This "fake location" becomes my current location.

But after 10-20 seconds, a mocking end. My "real location" becomes my current location. I have seen many examples on the Internet, and they use the same code that I use to mock a location. I do not know why this is happening with my application. Any help would be appreciated.

+8
android google-maps location
source share
3 answers

First of all, a location request will affect your layout location for the appropriate location provider.

If you create and set a location for the GPS provider, and then send the location request for the GPS provider again, the layout will be saved until the GPS equipment receives a new location. It may take 10-20 seconds or more (but after 10-20 seconds, the mocking ones end ...)

And also this question is the answer to some problems with fake location. You can see comments such as "I cannot get my current location after using this application!" On the market. Because they need a location request just like you.

And finally, please start a mocking operation in the service. I suggest doing this on a background thread, perhaps for every 1 or 2 seconds. This way you can reduce the impact of a possible location request for other applications.

 Handler mHandler; private void loopMocking(){ mHandler.post(mMockRunnable); } private Runnable mMockRunnable = new Runnable() { @Override public void run() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()); } newLocation.setTime(System.currentTimeMillis()); lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, newLocation); mHandler.postDelayed(mMockRunnable, 1000); // At each 1 second } }; 
+2
source share

Try stopping updates after setting a fake location.

 yourLocationManager.removeUpdates(yourLocationListener); 

For more information see https://developer.android.com/reference/android/location/LocationManager.html

0
source share

You need to stop receiving more location requests after setting a mock location.

use manager.removeUpdates (this). As the documentation says:

Deletes any current registration for location updates of current activity using this LocationListener. After this call, updates for this listener will no longer be. Consequently, your dummy location will remain set.

-one
source share

All Articles