0

Hey all, this will contain a few questions since I don't seem to really get it.

I have 1 class, the activity. which should display informations.

Then I have a background thread, extends runnable, which keeps getting new data (there for I didn't use AsyncTask, I could use it as a service, but since I hold a some critical resources in it, I would like not have it released when exiting the activity thread)

But I am in great doubt how to communicate between these 2.
First I thought of Intent, but these seem to be used mostly for launching other activities, or alike, and I need something permanent, since data will be in a steady flow.
Then I found out handler, but this doesn't seem to work when my thread is not an innerclass, so I'm thinking about either going back to the old Java observer pattern, if it's not possible to somehow pass the handler to the outerclass.

Any thoughts would be greatly appreciated

Sincerely Anders Metnik

2 Answers 2

0

There is a mechanism for your case - it is called handlers. Read more here.

As for having thread as inner class: Create your thread as a separate class, add a constructor with a handler parameter and pass it from your activity.

4
  • Did you even read my post, your did you just read the title? Because, it doesn't seem like it since I made a rather big comments about handlers Mar 24, 2011 at 9:52
  • Why it didn't work? Create your thread as a separate class, add a constructor with a handler parameter and pass it from your activity. Can't imagine where the problem can raise. Mar 24, 2011 at 9:53
  • Well, I had that as a suggestion, why didn't you just write that, in the first place. Mar 24, 2011 at 10:04
  • Sorry for my misattention. Updated the answer. Mar 24, 2011 at 10:06
0

Thread is not suppose to live outside Creator Activity Context, especially you want to preserve it out of Activity, better use Service (and manage the thread) to hold those data.

Intent is the best in terms of communicating between contexts. I think one of the scenario you can adopt is like this:

Application-class: holds those 'permanent' data you mentioned Service-class: Work (background) and send out "intents" to signal the update state of the operations Activity-class: Intent Receiver. Whenever intent signal received, grab the necessary data from the Application-class.

2
  • So if I get you right: 1. Backgroundthread into a Service 2. Implement Intents as communication between the 2 Right? Thanks in advance. Mar 24, 2011 at 9:54
  • Yes, and share data with some common static repository classes, if they are not easy to wrap in Intent Extra.
    – xandy
    Mar 25, 2011 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.