1 / 15

Cosc 5/4730

Cosc 5/4730. A little on threads and Messages: Handler class And AsyncTask. 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.

malha
Download Presentation

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. Cosc 5/4730 A little on threads and Messages: Handler class And AsyncTask

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

  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  private class processDataTaskextends AsyncTask<Data, Integer, Long> {     protected Long doInBackground(Data... datas) {int count = datas.length;         long totalSize = 0;         for (inti = 0; i < count; i++) {//do something with the data, then publish how far alongpublishProgress((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” Note, Data would be a class you created or some other collections (array/list)

  15. Q A &

More Related