views:

40

answers:

2

I have a background thread that is sending data about the application's current status to a server. The thread is supposed to constantly - or at least whenever the status changes - send this data.

The data is coming from the UI and the user is able to change the status any moment (by moving with the finger over the screen), so I need to tell the background thread to send the updated data.

Is there any Android way to do this? My solution would be to keep the thread's instance available and add some method to allow setting the thread's internal data that is to be send. However I'm not even sure if that even works (given that the thread doesn't run on the main thread where the command to update it would come from). I looked at AsyncTask before, as that seems to be the standard solution for background workers, but I didn't see anything that makes it possible to push data into that task (it only reports data/progress back to the UI thread).

A: 

Use a service..

Falmarri
From the reference: *“A Service is [...] representing either an application's desire to perform a longer-running operation while not interacting with the user [...]”*. So it's quite the contrary to what I'm looking for; I need direct interaction with the user's actions.
poke
No, you need to send that user interaction to a server. That's what a service is for. Let your application gather the information and then pass it to the service to do what it wants with it.
Falmarri
@Falmarri: The background process is supposed to do a bidirectional communication. I push data to the server but also get data from it.
poke
That sounds exactly what a service is useful for.
Falmarri
A: 

Can't your background worker check a thread-safe data structure every few milliseconds? The main thread just populates this and the background worker does what it wants with it.

Or just use a static variable surrounded by semaphores. Ugly but it would work.

Byron Whitlock