Question

I'm using a BroadcastReceiver and need to find the GPS location for the phone based on incoming SMS messages.

I'm using a pretty standard class that returns a LocationResult on a callback but if I use that in the BroadcastReceiver class then it can take too long and get killed by the OS.

I've tried using a service but it causes ANR problems if two events happen at the same time.

I've heard that uses threads in a BroadcastReceiver is a recipe for disaster so didn't even try that.

I'm now using an IntentService to queue up incoming requests and handle the GPS location finder. My question is how to use code with a callback so that it doesn't die when onHandleIntent finishes? I thought about using sleep instead but it doesn't seem like a polished solution.

Is there a better way to do this? I have to use a BroadcastReceiver but need a smart way of letting the GPS find the location over a 15-20 second period.

Any ideas would be most appreciated! (Android noob here).

Was it helpful?

Solution

I've tried using a service but it causes ANR problems if two events happen at the same time.

That means you are doing work on the main application thread, which is something you always want to avoid, including in services. Bear in mind that methods like onStartCommand() are called on the main application thread.

My question is how to use code with a callback so that it doesn't die when onHandleIntent finishes?

That is why IntentService is not a great answer for your problem. You need a regular Service, but one where you are doing work in your own background thread, are stopping the service when you are done, etc.

I have a proof-of-concept implementation of this in the form of LocationPoller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top