So, here is the code to send messages to a worker thread.
In your main thread create a worker thread using HandlerThread:
HandlerThread myThread = new HandlerThread("Worker
Thread");
myThread.start();
You will have to implement a Handler class for this worker thread.
Suppose it is called MyHandler as follows:
class MyHandler extends Handler {
public MyHandler(Looper myLooper) {
super(myLooper);
}
public void handleMessage(Message msg) {
Log.v("MyHandler", "handleMessage in worker
thread called");
}
}
Now, get the looper from that thread. And pass this looper to the
Handler class you implemented.
Looper mLooper = myThread.getLooper();
MyHandler mHandler = new MyHandler(mServiceLooper);
Now from your main thread when you have to pass a message to the
worker thread, do the following:
Message msg = mHandler.obtainMessage();
//msg.obj = blah blah
mHandler.sendMessage(msg);
-Cheers,
Tejas
On Apr 24, 11:58 pm, Tejas <[email protected]> wrote:
> I suppose, to pass data to a worker thread, that thread needs a
> Looper. Hence android has HandlerThread class which creates a looper
> for the worker thread.
> We can get this looper from the worker thread and then send messages
> to it.
>
> Please correct me if I'm wrong. I'll try to find some code
> demonstrating this. If anyone knows it already, please post a sample
> code.
>
> - Tejas
>
> On Apr 24, 11:43 pm, Anurag Singh <[email protected]> wrote:
>
>
>
>
>
> > Dianne, original poster want to pass stuff/data from main thread to
> > secondary thread.
>
> > is it possbile using Handler? Geaneraly, we use Handler to get back data
> > from secondary thread to main thread using SendMessage() or Post().
>
> > - Anurag Singh
>
> > > Don't do these things for threads running in the same process (which is
> > > what the original poster is asking about); much more overhead and
> > > complexity
> > > for no purpose.
>
> > > Handler is Android's standard way to get stuff back on the main thread.
> > > Create one in the main thread, then post messages or (Runnable objects if
> > > desired) to get what you want to that thread.
>
> > > The other Java concurrency stuff can be useful, but be careful because you
> > > don't want to end up with your main thread using that and blocking on one
> > > of
> > > the other threads for data from it. The main thread is message-based, so
> > > posting messages to it via Handler is a good approach.
>
> > > --
> > > Dianne Hackborn
> > > Android framework engineer
> > > [email protected]
>
> > > Note: please don't send private questions to me, as I don't have time to
> > > provide private support, and so won't reply to such e-mails. All such
> > > questions should be posted on public forums, where I and others can see
> > > and
> > > answer them.
>
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "Android Developers" group.
> > > To post to this group, send email to [email protected]
> > > To unsubscribe from this group, send email to
> > > [email protected]<android-developers%2Bunsubs
> > > [email protected]>
> > > For more options, visit this group at
> > >http://groups.google.com/group/android-developers?hl=en
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group
> > athttp://groups.google.com/group/android-developers?hl=en
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group
> athttp://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en