Using a looper is fairly straight forward. Supposed you want to keep
a 'worker' thread around to do various functions based on messages it
receives.
This is the way I generally do it:
class MyThread extends Thread
{
public Handler mHandler;
public static final int DO_SOMETHING = 1;
public static final int DO_SOMETHING_ELSE = 2;
@Override public void run()
{
Looper.prepare();
mHandler = new Handler()
{
public void handleMessage(Message msg)
{
Bundle bundle = msg.getData();
int type = bundle.getInt("TYPE");
switch (type)
{
case DO_SOMETHING:
do_something();
break;
case DO_SOMETHING_ELSE:
do_something_else();
break;
...
}
}
};
Looper.loop();
}
}
Then from the main thread...
MyThread mThread = new MyThread();
mThread.start();
//worker thread is now waiting for messages....
//make it do something...
Bundle mBundle = new Bundle();
Message msg = Message.obtain();
mBundle.putInt("TYPE", MyThread.DO_SOMETHING);
msg.setData(mBundle);
mThread.mHandler.sendMessage(msg);
//make it do something else...
mBundle.clear();
mBundle.putInt("TYPE", MyThread.DO_SOMETHING_ELSE);
msg.setData(mBundle);
mThread.mHandler.sendMessage(msg);
On Sat, Apr 24, 2010 at 8: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 at
> http://groups.google.com/group/android-developers?hl=en
--
http://diastrofunk.com, http://developingthedream.blogspot.com/,
http://www.youtube.com/user/revoltingx, ~Isaiah 55:8-9
--
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