0

I want to get Latitude and Longitude as and when location is changed.How it can be possible using Intentservice? other thing is that even application is in background I want current Latitude and Longitude continuously.

Nitish Patel
  • 1,026
  • 2
  • 19
  • 36

3 Answers3

2

You can try getLastKnownLocation(), but it is likely that it will be null. And an IntentService cannot wait around for some location fix to arrive.

Rather, you need a regular Service, crafted to handle this scenario. use cwac-locpoll instead

Anil
  • 1,028
  • 9
  • 20
0

You will need to implement LocationListner in a service. Here is a link that has more information. Location Listener in Background Service Android and Location listener in a service sends notification issue

In case you dont want to use a service: BroadcastReceiver for location You need to implement a custom broadcast receiver.

Community
  • 1
  • 1
Sudhee
  • 704
  • 6
  • 12
  • I don't want to use service because it drains battery.How can I get same result which as you suggest me using service,but I want same result using Intentservice.Is it possible to get same result as service gives? – Nitish Patel Apr 29 '13 at 12:50
  • Try this:http://stackoverflow.com/questions/5240246/broadcastreceiver-for-location – Sudhee Apr 29 '13 at 13:09
0

if you really have to use a IntentService, you can implement a loop with an appropriate timeout. use onLocationChanged to change some values. the loop can be testing this values every one second. if values change, exit loop, if timeout is reached exit loop. The loop will keep the IntentService alive waiting for onLocationChanged to fire.

hyena
  • 755
  • 1
  • 14
  • 24
  • found a better way to do this here http://stackoverflow.com/questions/6866415/intentservice-how-to-enqueue-correctly – hyena Oct 28 '14 at 06:13
  • I tried this answer but onlocationchanged never is fired, but it does work if you do the same code in a frangment or activity, I believe you cant use this "trick" in IntentService. – angel Aug 03 '16 at 19:24
  • @angel, get the location using a new thread then use thread.join(10000); – hyena Aug 07 '16 at 03:06
  • @angel, get the location using a new thread then use thread.join(10000); to join the current thread with the new one. create a call back method that will be used in the onlocationchanged to capture the current location attributes – hyena Aug 07 '16 at 03:15