1 / 11

Cosc 5/4730

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.

nerita
Télécharger la présentation

Cosc 5/4730

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cosc5/4730 A little on threads and Messages: Handler class

  2. 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.

  3. 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.

  4. 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);

  5. 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);

  6. 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);

  7. 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.

  8. 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);

  9. 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.

  10. 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.

  11. Q A &

More Related