OnLocationChanged always returns i old location

I registered my LocationManager for location updates every 10 seconds

mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10 * 1000, 50, this); 

But the onLocationChanged returns a location every 10 seconds, which (location) is more than two hours. And this label never changes.

The problem is this:

After 2 hours, I was in another place ( home ), where I used the device on Wi-Fi. Now I’m in a different place ( office ) on another Wi-Fi, where my application shows my current location as home . The same thing happened at home yesterday when he showed the office as my current location. He got to work (started showing the correct location) when I closed the application, opened the FourSquare application and re-opened my application.

Full code:

 public class LocationService extends Service implements LocationListener { public static double curLat = 0.0; public static double curLng = 0.0; private LocationManager mgr; private String best; private Location location; @Override public IBinder onBind(Intent arg0) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { mgr = (LocationManager) getSystemService(LOCATION_SERVICE); best = LocationManager.NETWORK_PROVIDER; location = mgr.getLastKnownLocation(best); if (location != null) { dumpLocation(location); mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 * 1000, 50, this); } return START_NOT_STICKY; } } private void dumpLocation(Location l) { SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy:hh:mm:ss", Locale.ENGLISH); String format = s.format(l.getTime()); //The above time is always 28/03/2013:09:26:41 which is more than 2 hrs old curLat = l.getLatitude(); curLng = l.getLongitude(); } @Override public void onLocationChanged(Location location) { dumpLocation(location); } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } } 

Begins in Activities as follows:

 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); Intent i = new Intent(this, LocationService.class); pi = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); am.cancel(pi); am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 10000, pi); 

Permissions in the manifest:

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> 

Now I can get the correct location by opening another location-based application such as Maps, Navigator, Foursquare, etc. But why my application cannot receive a new / new patch from the provider.

thanks

+8
android locationmanager
source share
2 answers

You get the old location because of this line

  location = mgr.getLastKnownLocation(best); 

because if GPS is not turned on, it will show you the old location. So remove this code. He will work as a champion. You can also refer to this library.

https://github.com/nagendraksrivastava/Android-Location-Tracking-Library

Based on your comments, I edited the answer. Good. Let me explain line by line location = mgr.getLastKnownLocation(best); it will provide you with the object of the last iconic location, after which it will enter the if state, and it will call dumplocation and get the latest location data, and after that you called

 mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 * 1000, 50, this); 

but suppose the GPS provider is disabled, then it will not get the new location, so you only get the old location. So either you can change it as

 if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER,new NagendraSingleLocationListener(),null); } else { locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER,new NagendraSingleLocationListener(),null); } 
+5
source share

I think because you cancel your pending intent right away, so requestLocationUpdate will not start the update before you cancel it. Why don't you sleep, maybe 2 seconds before cancellation.

0
source share

All Articles