4

I want to know right now if the GPS is turned on and fixed.

I don't want to listen to events - just to get an answer (And if it is fix - get the location).

In all the examples people create a listener and wait for events from the GPS. my call should return immediately.

Anything above level 11 is OK, but if something exists only on level 14 and above I can manage.

Thanks.

Dani
  • 14,639
  • 11
  • 62
  • 110
  • Check this answer, I think this is what you're looking for: http://stackoverflow.com/a/3712727/1193549 – hunyadym Jul 16 '13 at 08:31

1 Answers1

5

I don't want to listen to events - just to get an answer

You can use getLastKnownLocation() but if this returns null then you must wait for a location update.


To test if the GPS is active use isProviderEnabled().

You should consider using the PASSIVE_PROVIDER as well. This report any location fix whether it's GPS, NETWORK, etc.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • But it will give me any last known location. it is important for me to know right now if the GPS is on and fixed. if the last known location is from 10 min ago it is not good. – Dani Jan 01 '13 at 17:54
  • 1
    To be clear this does not check if the GPS is active, you should use `isProviderEnabled()`. `getLastKnownLocation()` should return the last fix acquired, but if your user just emerged from one of Norway's 20km+ long tunnels the data will be stale. You can check the timestamp of this Location object like any other with `getTime()`. – Sam Jan 01 '13 at 18:00
  • So the only way is to check the timestamp ? no other method to verify if the GPS is locked ? – Dani Jan 01 '13 at 18:22
  • I will try to look for this one myself - but if I use this getTime() to compare to the local device time, how will it be effected by the TimeZone of the device ? is there a TimeZone for GPS time ? – Dani Jan 01 '13 at 18:27
  • I'm sorry there is no silver bullet for what you want. Either `getLastKnownLocation()` has the information you want or you must request an update then listener for the response... – Sam Jan 01 '13 at 18:27
  • "is there a TimeZone for GPS time ?" Hmm, `getTime()` should reference the device's timezone, so you'll have to ask the device what timezone it is in, I don't think the GPS duplicates this interface. But this is my gut response, I'll double check myself and let you know if I find anything different. – Sam Jan 01 '13 at 18:30
  • Thanks, I'll go this way, hopefully they will have this in future versions, I know some people count satellites but this is not exact as well... Will look forward to here about the Time Zone issue if you happen to check it. – Dani Jan 01 '13 at 18:30