Communic with handle

Put your problem here if it does not fit any of the other categories.

Communic with handle

Postby ruben1988 » Thu Jan 20, 2011 12:00 am

Hi. Sorry for my bad English. If I had this class code:

Code: Select all
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();
  }
}



and have a MainActivity, how can I communic with this handle? I have this code but I cant communic with handle
Code: Select all
Bundle mBundle = new Bundle();
Message msg = Message.obtain();

mBundle.putInt("TYPE", MyThread.DO_SOMETHING);
msg.setData(mBundle);

mThread.mHandler.sendMessage(msg);

ruben1988
Freshman
Freshman
 
Posts: 5
Joined: Sat Jan 08, 2011 11:27 am

Top

Re: Communic with handle

Postby jonbonazza » Thu Jan 20, 2011 2:02 am

Declare the handle in the Activity class, not the Thread subclass.

Then, when you want to post to the UI thread, you would use:
mHandler.sendMessage(blah blah blah);
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 17 guests