AsyncTask:
AsyncTask is an abstract Android class which helps the Android applications to handle the Main UI thread in efficient way. AsyncTask class allows us to perform long lasting tasks/background operations and show the result on the UI thread without affecting the main thread.
How to implement AsyncTask in Android applications?
1. Create a new class inside Activity class and subclass it by extending AsyncTask as shown below
private class DownloadMp3Task extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
//Yet to code
}
protected void onProgressUpdate(Integer... progress) {
//Yet to code
}
protected void onPostExecute(Long result) {
//Yet to code
}
}
2. Execute the task simply by invoking execute method as shown below:
1 new DownloadMp3Task().execute(mp3URL);
AsyncTask – The 4 steps
1. onPreExecute
2. doInBackground
3. onProgressUpdate
4. onPostExecute
OnPreExecute:
DoInBackground:
OnProgressUpdate:
OnPostExecute:
AsyncTask – Rules to be followed:
1. The AsyncTask class must be loaded on the UI thread.
2. The task instance must be created on the UI thread.
3. Method execute(Params…) must be invoked on the UI thread.
4. Should not call onPreExecute(), onPostExecute(Result), doInBackground(Params…), onProgressUpdate(Progress…) manually.
5. The task can be executed only once (an exception will be thrown if a second execution is attempted.)
iPrism Technologies is a one of the leading Android and iOS mobile app's development company. We have a well versed and dedicated team to develop feature rich mobile applications to cater to your needs.
AsyncTask is an abstract Android class which helps the Android applications to handle the Main UI thread in efficient way. AsyncTask class allows us to perform long lasting tasks/background operations and show the result on the UI thread without affecting the main thread.
How to implement AsyncTask in Android applications?
1. Create a new class inside Activity class and subclass it by extending AsyncTask as shown below
private class DownloadMp3Task extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
//Yet to code
}
protected void onProgressUpdate(Integer... progress) {
//Yet to code
}
protected void onPostExecute(Long result) {
//Yet to code
}
}
2. Execute the task simply by invoking execute method as shown below:
1 new DownloadMp3Task().execute(mp3URL);
AsyncTask – The 4 steps
1. onPreExecute
2. doInBackground
3. onProgressUpdate
4. onPostExecute
OnPreExecute:
Invoked before the task is executed ideally before doInBackground method is called on the UI thread. This method is normally used to setup the task like showing progress bar in the UI.
DoInBackground:
Code running for long lasting time should be put in doInBackground method. When execute method is called in UI main thread, this method is called with the parameters passed.
OnProgressUpdate:
Invoked by calling publishProgress at anytime from doInBackground. This method can be used to display any form of progress in the user interface.
OnPostExecute:
Invoked after background computation in doInBackground method completes processing. Result of the doInBackground is passed to this method.
AsyncTask – Rules to be followed:
1. The AsyncTask class must be loaded on the UI thread.
2. The task instance must be created on the UI thread.
3. Method execute(Params…) must be invoked on the UI thread.
4. Should not call onPreExecute(), onPostExecute(Result), doInBackground(Params…), onProgressUpdate(Progress…) manually.
5. The task can be executed only once (an exception will be thrown if a second execution is attempted.)
iPrism Technologies is a one of the leading Android and iOS mobile app's development company. We have a well versed and dedicated team to develop feature rich mobile applications to cater to your needs.
No comments:
Post a Comment