Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threads, Handlers, and AsyncTasks

Similar presentations


Presentation on theme: "Threads, Handlers, and AsyncTasks"— Presentation transcript:

1 Threads, Handlers, and AsyncTasks
Cosc 5/4730 Threads, Handlers, and AsyncTasks

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. And services, plus some listeners are also threads. 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. A quick note, you can pass a handler to another activity/fragment as well, so you can use it from other "locations" as well.

8 Sending lots of information
OnCreate() handler = new Handler() 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. In the Running thread
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 AsyncTask

12 AsyncTask This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. AsyncTasks should ideally be used for short operations (a few seconds at the most.) otherwise you should use threads and handlers. A note, asyncTasks don't have a "pause" or "notify" method.

13 AsyncTask (2) An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute. doInBackground runs in the background. onProgressUpdate and onPostExecute are executed on the main/UI thread Meaning they can also update the widgets. The return value from doInBackground is called as parameter to onPostExecute publishProgress (called in doInBackground) invokes the onProgressUpdate

14 AsyncTask Example Data is information to be processed
 private class processDataTask extends AsyncTask<Data, Integer, Long> {      protected Long doInBackground(Data... datas) {          int count = datas.length;          long totalSize = 0;          for (int i = 0; i < count; i++) {              //do something with the data, then publish how far along              publishProgress((int) ((i / (float) count) * 100));              // Escape early if cancel() is called              if (isCancelled()) break;          }          return totalSize; //amount of data process maybe?      } //background thread      protected void onProgressUpdate(Integer... progress) {          setProgressPercent(progress[0]); //update UI      } //UI thread      protected void onPostExecute(Long result) {          showDialog("Downloaded " + result + " bytes");      } //UI thread  } Once created, a task is executed very simply: new processDataTask().execute(Data1, Data2, Data3); Data is information to be processed Integer is the value for publishProgress and onProgressUpdate And Long is the return value and parameter to onPostExecute The call, uses Data to create the "list" So Data1, Data2, Data3 becomes an list of type Data Note, Data could be a class you created or some other collections (array/list)

15 Q A &


Download ppt "Threads, Handlers, and AsyncTasks"

Similar presentations


Ads by Google