cosc 5 4730
Download
Skip this Video
Download Presentation
Cosc 5/4730

Loading in 2 Seconds...

play fullscreen
1 / 11

Cosc 5/4730 - PowerPoint PPT Presentation


  • 82 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Cosc 5/4730' - nerita


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cosc 5 4730

Cosc5/4730

A little on threads and Messages:

Handler class

messages
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
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 messages1
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
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
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
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 information1
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
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
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.

slide11
Q

A

&

ad