Search code examples
androidgpsgoogle-maps-android-api-2locationlistener

onGpsStatusChanged alternatives?


Posted question recently about why my onGpsStatusChanged is called only once and not called anymore, got no answers so will assume solution is unknown (been trying it out for 2 days), so is there perhaps any alternative solution to detect if GPS signal was lost and no location updates are available?

I am running LocationListener in background service.

I need to detect when user lost GPS -> Start alarmManager for 5 minutes and if GPS appears online again -> cancel alarm.

The only problem I have detecting if gps was lost or not.


Solution

  • Loosing GPS just means that you will receive no more location updates. So why not just setting the Alarm again with each location update you receive. The set-Methods of the AlarmManager are automatically cancelling Alarms previously set with the same PendingIntent. So you will have only one Alarm set all the time. As soon as no new location is received for longer than 5 minutes your Alarm will be executed.

    May be setting the Alarm again every second is too expensive (I have no idea about that). In this case, add a counter, that resets the AlarmManager only every n-th location update. The idea is still the same.