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());
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