filmov
tv
Android Studio - Complete Android Apps Tutorial - Thread and Async Task - Day 13 - 9.01

Показать описание
Thread and AsyncTask
2
Why Another Thread? Failure to return from an Activity callback method within a few seconds 5 secs will cause Android to think your application is stuck Pops an error dialog ANR Kills your activity Even delays shorter than a few seconds will let user feels that the application is unresponsive/slow/lag
3
Handler Only the main thread/UI thread can update the UI, other threads updating the UI will cause CalledFromWrongThreadException If background thread needs to update the UI, request the UI thread to do the update via a Handler Construct a Handler in the main thread Handler mHandler = new Handler;
4
Why Another Thread? Possible reasons: Connecting to Internet to fetch data XML/JSON Since API Level 11, this will cause NetworkOnMainThreadException Reading or writing to a removable storage Many SQLite database operations Lengthy mathematical computations involving many loops
5
Application Not Responding ANR dialog
6
7
Monitoring Threads Via Android Device Monitor
8
Thread Construct a new Thread Override run in the Thread and the task in run will be run in another thread Start the thread with start new Thread public void run // background task ; .start;
9
Handler Only the main thread/UI thread can update the UI, other threads updating the UI will cause CalledFromWrongThreadException If background thread needs to update the UI, request the UI thread to do the update via a Handler Construct a Handler in the main thread Handler mHandler = new Handler;
10
11
12
13
AsyncTask Bundles thread creation and inter-thread communication in one class Extends and override all runs in UI thread except doInBackground onPreExecute : after the task is executed doInBackgroundParams... : after onPreExecute onProgressUpdateProgress... : after publishProgressProgress... in doInBackground onPostExecuteResult : after completion of doInBackground Create an instance and executeParams...
14
Extends AsyncTask to obtain an inner class AsyncTask is defined by 3 generic types Params, Progress, Result e.g. String, Integer, Boolean, Void, Void, Void Params: the type of the parameters sent to the task upon execution Progress: the type of the progress units published during the background computation Result: the type of the result of the background computation
15
class RefreshTask extends AsyncTaskVoid, Void, Void
@Override protected Void doInBackgroundVoid... params // long running process
Using AsyncTask
16
class RefreshTask extends AsyncTaskString, Void, Boolean
@Override protected Boolean doInBackgroundString... params String url = params0; // get the 1st parameter // long running process return true;
@Override protected Void onPostExecuteBoolean result ifresult // handle successful task
Using AsyncTask with parameters
17
class RefreshTask extends AsyncTaskVoid, Integer, Void
@Override protected Void doInBackgroundVoid... params // long running process publishProgresscount; // long running process
@Override protected Void onProgressUpdateInteger... values int progress = values0; // update the progress bar
Using AsyncTask – publish progress
18
2
Why Another Thread? Failure to return from an Activity callback method within a few seconds 5 secs will cause Android to think your application is stuck Pops an error dialog ANR Kills your activity Even delays shorter than a few seconds will let user feels that the application is unresponsive/slow/lag
3
Handler Only the main thread/UI thread can update the UI, other threads updating the UI will cause CalledFromWrongThreadException If background thread needs to update the UI, request the UI thread to do the update via a Handler Construct a Handler in the main thread Handler mHandler = new Handler;
4
Why Another Thread? Possible reasons: Connecting to Internet to fetch data XML/JSON Since API Level 11, this will cause NetworkOnMainThreadException Reading or writing to a removable storage Many SQLite database operations Lengthy mathematical computations involving many loops
5
Application Not Responding ANR dialog
6
7
Monitoring Threads Via Android Device Monitor
8
Thread Construct a new Thread Override run in the Thread and the task in run will be run in another thread Start the thread with start new Thread public void run // background task ; .start;
9
Handler Only the main thread/UI thread can update the UI, other threads updating the UI will cause CalledFromWrongThreadException If background thread needs to update the UI, request the UI thread to do the update via a Handler Construct a Handler in the main thread Handler mHandler = new Handler;
10
11
12
13
AsyncTask Bundles thread creation and inter-thread communication in one class Extends and override all runs in UI thread except doInBackground onPreExecute : after the task is executed doInBackgroundParams... : after onPreExecute onProgressUpdateProgress... : after publishProgressProgress... in doInBackground onPostExecuteResult : after completion of doInBackground Create an instance and executeParams...
14
Extends AsyncTask to obtain an inner class AsyncTask is defined by 3 generic types Params, Progress, Result e.g. String, Integer, Boolean, Void, Void, Void Params: the type of the parameters sent to the task upon execution Progress: the type of the progress units published during the background computation Result: the type of the result of the background computation
15
class RefreshTask extends AsyncTaskVoid, Void, Void
@Override protected Void doInBackgroundVoid... params // long running process
Using AsyncTask
16
class RefreshTask extends AsyncTaskString, Void, Boolean
@Override protected Boolean doInBackgroundString... params String url = params0; // get the 1st parameter // long running process return true;
@Override protected Void onPostExecuteBoolean result ifresult // handle successful task
Using AsyncTask with parameters
17
class RefreshTask extends AsyncTaskVoid, Integer, Void
@Override protected Void doInBackgroundVoid... params // long running process publishProgresscount; // long running process
@Override protected Void onProgressUpdateInteger... values int progress = values0; // update the progress bar
Using AsyncTask – publish progress
18