Search code examples
androidgeolocationgpslocationlistener

GPS is working the whole time even after getting a location


I am developing an app that uses the gps which runs until app is in service. My GPS wakes up after 5 mins and gets the location then its symbol stops blinking and becomes invisible in almost all devices (Sony xperia, Samsung tab, Htc WildFire, Motorola photon) but not in Motorola Defy and Samsung Ace, which consume huge battery power. GPS gets a fix and becomes stable and visible the whole time. The way the GPS of these devices work (Moto Defy and Samsung Ace) is different. Look at my code:

long _listenerWakeUpTimeInterval=1000*60*5;**  //5 minutes
_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, _listenerWakeUpTimeInterval, 300, locationListener);

The GPS of all devices wake up after 5 minutes but the GPS of these two devices (Moto Defy and Samsung Ace) calls the onLocationChanged(Location loc) method every 300 meters. The time matters not for these two devices. When I travel fast (in a car) this distance is covered in a few minutes (1 or 2 minutes). So I get a call on onLocationChanged(Location loc) method. And in these devices the GPS is always fixed, static and visible. I don't want to call locationManager.removesupdates methods every time when the GPS gets a fix. I only cal this method in onDestroy() of service. So please tell why the GPS of these two devices behaves differently from other devices, and how to solve this problem ?


Solution

  • I got the solution of this problem.I use AlarmManager(repeating alarm after each 5 minutes) to register gps and then I run my Gps for 30 seconds and unregister Gps .If I got call in onLocationChanged(Location loc) then I unregister Gps in this method.otherwise(If i don't get new location) I unregister Gps through handler after 30 seconds.When I need Gps again I follow the same process.By this way my Gps works max 30 seconds not for whole the time until service run.