Exegesis
Exegesis

Reputation: 1078

Android GPS Service Issue

I have a service (with a wakelock) that must run continuously behind the scenes capturing user Geo Location. The Service implements the LocationListener methods (i.e. onLocationChanged()).

However it takes some time for onLocationChanged() to get invoked by the phone, so in the meantime my service has to do something. I thought of using Thread.sleep(), but will that prevent the phone from invoking onLocationChanged()? Or should I do polling: while(i < 1000,000) {++i;}?

I'm not getting such abundant GPS results using either of those ideas; wondering if anybody can give me a tip on how to accomplish this.

Upvotes: 0

Views: 318

Answers (3)

Exegesis
Exegesis

Reputation: 1078

I solved the problem by removing WakeLock (waste of battery), doing a 10-second busy wait/Thread Sleep (just in case one blocks the GPS invocation of onLoc()), and using AlarmManager to wake the device from sleep and start the service.

This: gets GPS during sleep AND doesn't drain battery power.

Upvotes: 0

santosh
santosh

Reputation: 112

I guess you want to keep the service "alive" while it is waiting for location changed information. That is taken care of by the system and you do not have to add code for that. When location change information becomes available, the onLocationChanged() would be invoked in the context of your service.

Upvotes: 1

lulumeya
lulumeya

Reputation: 1628

I think you can use wait(), and notify() with synchronize block with LocationListener instance. search for samples using wait(), notify().

Upvotes: 1

Related Questions