Android Threading

17
Android Threading Jussi Pohjolainen Tampere University of Applied Sciencs

Transcript of Android Threading

Page 1: Android Threading

Android  Threading  

Jussi  Pohjolainen  Tampere  University  of  Applied  Sciencs  

Page 2: Android Threading

UI  Thread  

•  When  Android  app  is  launched  one  thread  is  created.  This  thread  is  called  Main  Thread  or  UI  Thread  

•  UI  Thread  is  responsible  for  dispatching  events  to  widgets  

•  Avoid  doing  0me  consuming  tasks  in  UI  Thread  since  it  leads  to  app  that  does  not  respond  quickly  

Page 3: Android Threading

TesAng  UI  Responsivess  public class ThreadExample extends Activity implements OnClickListener {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Button button = new Button(this);

button.setText("Do Time Consuming task!");

setContentView(button);

button.setOnClickListener(this);

}

@Override

public void onClick(View v) {

try {

for(int i=0; i<10; i++) {

System.out.println(i);

Thread.sleep(10000);

}

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

Page 4: Android Threading

Result  

Page 5: Android Threading

TesAng  Separate  Thread  public class ThreadExample extends Activity implements OnClickListener, Runnable {

...

@Override

public void onClick(View v) {

Thread thread = new Thread(this);

thread.start();

}

@Override

public void run() {

try {

for(int i=0; i<10; i++) {

System.out.println(i);

Thread.sleep(10000);

}

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

Page 6: Android Threading
Page 7: Android Threading

How  about  influencing  the  UI?   ... @Override public void run() { try { for(int i=0; i<10; i++) { button.setText("Iteration: " + i); Thread.sleep(1000); } } catch (InterruptedException e) { e.printStackTrace(); } } ...

Page 8: Android Threading

Problem  

Page 9: Android Threading

Why?  

•  Android  UI  Toolkit  is  not  thread  safe  •  If  you  want  to  manipulate  UI,  you  must  do  it  inside  the  UI  thread  

•  How  do  you  do  it  then?  You  can  use  – Activity.runOnUiThread(Runnable) – View.post(Runnable) – View.postDelayed(Runnable, long) – …

Page 10: Android Threading

Activity.runOnUiThread(Runnable)

•  The  given  acAon  (Runnable)  is  executed  immediately  if  current  thread  is  UI  thread  

•  If  current  thread  is  NOT  UI  thread,  the  acAon  (Runnable)  is  posted  to  event  queue  of  the  UI  Thread  

Page 11: Android Threading

Example  of  RunOnUiThread  public class ThreadExample extends Activity implements OnClickListener, Runnable {

...

@Override

public void onClick(View v) {

Thread t = new Thread(this);

t.start();

}

public void run() {

// lengthy operation

try {

Thread.sleep(2000);

} catch (InterruptedException e) { }

runOnUiThread(new Update());

}

class Update implements Runnable {

// This action is posted to event queue

public void run() {

button.setText("Finished!");

}

}

}

Page 12: Android Threading

View.post(Runnable) View.postDelayed(Runnable, Long)

•  These  methods  are  of  view  and  are  use  for  updaAng  the  view  

•  AcAon  (Runnable)  is  placed  on  Message  Queue  

•  Runnable  acAon  runs  on  UI  Thread  •  postDelayed  method  for  delayed  acAon  

Page 13: Android Threading

Using  post   private int iteration; ... @Override

public void run() { try { for(int i=0; i<10; i++) { iteration = i; button.post(new InfluenceUIThread()); Thread.sleep(1000); } } catch (InterruptedException e) { e.printStackTrace(); } } class InfluenceUIThread implements Runnable { public void run() { button.setText("Iteration = " + iteration); } }

Page 14: Android Threading

Using  Anonymous  Inner  Classes   @Override

public void onClick(View v) { new Thread(new Runnable() { public void run() { try { for(int i=0; i<10; i++) { iteration = i; button.post(new Runnable() { public void run() { button.setText("Iteration = " + iteration); } }); Thread.sleep(1000); } } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); }

This  can  be  really  confusing...  

Page 15: Android Threading

AsyncTask

•  Goal:  take  care  thread  management  for  you  •  Use  it  by  subclassing  it:  class  MyTask  extends  AsyncTask  

•  Override  onPreExecute(),  onPostExecute()  and  onProgressUpdate() –  Invokes  in  UI  Thread  

•  Override  doInBackground() –  Invokes  in  worker  thread  

Page 16: Android Threading

Example  (Google  SDK)  private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {      protected Long doInBackground(URL... urls) {          int count = urls.length;          long totalSize = 0;          for (int i = 0; i < count; i++) {              totalSize += Downloader.downloadFile(urls[i]);              publishProgress((int) ((i / (float) count) * 100));          }          return totalSize;      }      protected void onProgressUpdate(Integer... progress) {          setProgressPercent(progress[0]);      }      protected void onPostExecute(Long result) {          showDialog("Downloaded " + result + " bytes");      }  }  new DownloadFilesTask().execute(url1, url2, url3);

Params,  Progress,  Result  

Page 17: Android Threading

public class ThreadExample extends Activity implements OnClickListener {

private Button button;

...

class MyBackgroundTask extends AsyncTask<Integer, Integer, Integer> {

protected Integer doInBackground(Integer... ints) {

int i = ints[0];

try {

for(i=0; i<10; i++) {

System.out.println("doInBackground!");

publishProgress(new Integer(i));

Thread.sleep(1000);

}

} catch(Exception e) {

e.printStackTrace();

}

return i;

}

protected void onProgressUpdate(Integer iteration) {

button.setText("Iteration = " + iteration);

}

protected void onPostExecute(Integer result) {

button.setText("Finished with result of: " + result);

}

}

}