Android: how to fix gps with false (fake) coordinates?

I try to program the GPS programmatically when the GPS signal is not available. I want to do this in order to provide mock coordinates for other applications such as Google maps and others. I tried using GPS_PROVIDER and NETWORK_PROVIDER, but only with the latter I can get a new location for google maps. But this only works with the first tim, and if I want to redo more false locations, Google maps do not see any changes ... why? Should I do the service for what I want?

public void setLocation(double latitude, double longitude) { //mocLocationProvider = LocationManager.GPS_PROVIDER; mocLocationProvider = LocationManager.NETWORK_PROVIDER; lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); lm.clearTestProviderEnabled(mocLocationProvider); lm.requestLocationUpdates(mocLocationProvider,0,0,new llistener()); lm.addTestProvider(mocLocationProvider, false, false, false, false, false, false, false, 0, 10); lm.setTestProviderEnabled(mocLocationProvider, true); Location loc = new Location(mocLocationProvider); loc.setTime(System.currentTimeMillis()); loc.setLatitude(latitude); loc.setLongitude(longitude); loc.setAccuracy(10); lm.setTestProviderStatus(mocLocationProvider, LocationProvider.AVAILABLE,null, System.currentTimeMillis()); lm.setTestProviderLocation(mocLocationProvider, loc); } 

and location listener

 public class llistener implements LocationListener { @Override public void onLocationChanged(Location loc) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } } 

thanks

+4
source share
1 answer

What false locations are needed:

 <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"> 

Also, in the settings of your Android phone, make sure that the "Allow layouts" checkbox is checked

 locationManager.addTestProvider(mocLocationProvider, false, false, false, false, true, true, true, 0, 5); locationManager.setTestProviderEnabled(mocLocationProvider, true); 

. Now that you want to send the location, call this code:

 Location mockLocation = new Location(mocLocationProvider); // a string mockLocation.setLatitude(location.getLatitude()); // double mockLocation.setLongitude(location.getLongitude()); mockLocation.setAltitude(location.getAltitude()); mockLocation.setTime(System.currentTimeMillis()); locationManager.setTestProviderLocation( mocLocationProvider, mockLocation); 

.

But this only works for the first time

Because you called him once.

I need to make a service for what I want to do

You can do it, even AsyncTask will do

do it or not: lm.requestLocationUpdates ()

Yes you should with mocLocationProvider

I already call the setLocation method (latitude, longitude) many times, but it only works (update location) for the first time for google maps

There is no method called setLocation() , use setTestLocation()

What should I do in asynckTask

You can use Thread, TimerTask, or whatever you like. The idea is to enter the location in the Frame each time.

+18
source

Source: https://habr.com/ru/post/1380093/


All Articles