110 likes | 223 Views
Cosc 5/4730. A little on threads and Messages: Handler class. Messages. Only the activity thread can change the “screen” widgets. So if you start a thread up, then you have send messages back to main thread to change a widget. So you need to pieces.
E N D
Cosc5/4730 A little on threads and Messages: Handler class
Messages. • Only the activity thread can change the “screen” widgets. • So if you start a thread up, then you have send messages back to main thread to change a widget. • So you need to pieces. • A message handler (normally in Oncreate() ) to receive messages • And a way to send those messages.
Simple messages • In many cases you may only need to send a message, more like a “poke”. • “Do something, you know that one!” kind of message • Hander.sendEmptyMessage(int); • Where the receiving side, gets a number.
Simple messages OnCreate() handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0) { //do whatever msg 0 is. } //check for other message as needed. } • Thread handler.sendEmptyMessage(0);
Message with a little information. • In the handle you can get a Message object • Message msg = handler.obtainMessage(); • Message has a “what” like the simple message • and int arg0, arg1 • So you can send two pieces of integer information. • Then you send that message • Handler.sendMessage(msg);
information messages OnCreate() handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 1) { int arg1 = msg.arg1; Int arg2 = msg.arg2; } //check for other message as needed. } • Thread Message msg = handler.obtainMessage(); //setup the message msg.what= 1; msg.arg1 = 1; msg.arg2= 3012; handler.sendMessage(msg);
Sending lots of information. • Besides the two arg variables, you can set a bundle in the message as well. • So if you want send more then two integer • Or you want to send any other type, say strings.
Sending lots of information OnCreate() handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 3) { Blunde stuff = msg.getData(); str1 = stuff.getString(“key2”); … } //check for other message as needed. } • Thread Message msg = handler.obtainMessage(); Bundle b = new Bundle(); b.putString(“key”, “Stuff”); b.putString(“key2”, “more Stuff”); //setup the message number msg.what= 3; Msg.setData(b); //add bundle handler.sendMessage(msg);
A note on threads • Pausing threads. • If you want a running thread to “pause” and then start up again later. • Use the wait and notify methods in the Thread class. • In the Running thread • It calls wait(); • Then when the thread is to be woken up • Notify is called.
Example • Main thread MyThread = new Thread (this); myThread.start(); … //wake up a thread pause = false; synchronized(myThread) { //wake up 1 thread myThread.notify(); // or myThread.notifyAll() to wake up all threads. } • Thread If (pause) { //boolean try { synchronized(myThread) { myThread.wait(); } } catch (InterruptedException e) { ;// failed to wait! } }//end if //when notified, thread starts here. The ThreadDemo has working version of all this.
Q A &