What is AsyncTask?
When to use AsyncTask?
Android implements single thread model and whenever anandroid application is launched, a thread is created. Assuming we are doing network operation on a button click in our application. On button click a request would be made to the server and response will be awaited. Due to single thread model of android, till the time response is awaited our screen is non-responsive. So we should avoid performing long running operations on the UI thread. This includes file and network access.
Android implements single thread model and whenever an
To overcome this we can create new thread and implement run method to perform this network call, so UI remains responsive.
But since Android follows single thread model and Android UI toolkit is not thread safe, so if there is a need to make some change to the UI based on the result of the operation performed, then this approach may
So the Android framework has given a very good pattern which is enveloped into AsyncTask.
Note: AsyncTask should ideally be used for operations that take
AsyncTask has four steps:
doInBackground : Code performinglong running operation goesin this method. WhenonClick method is executed on click ofbutton , it calls execute method which accepts parameters and automatically callsdoInBackground method with the parameters passed.onPostExecute : This method is called afterdoInBackground method completes processing. Result fromdoInBackground is passed to this method.onPreExecute : This method is called beforedoInBackground method is called.onProgressUpdate : This method is invoked by callingpublishProgress anytime fromdoInBackground call this method.
The task can be cancelled by invoking cancel( boolean) method. This will cause subsequent calls to isCancelled ( ) to return true. After invoking this method, onCancelled ( Object) method is called instead of onPostExecute ( ) after doInBackground ( ) returns.
How to use AyncTask? Example application
In this sample application I just made the process to sleep for some period of time instead of doing network operation. ( Just to explain the concept of AsyncTask. This is not a realtime application).
In the UI thread user enters a value (time in milli seconds ) which will be passed to AsyncTaskRunner.
When the user clicks on the Run Async Task button, we make the process to sleep for give period of time. At the same time we keep the UI thread responsive by showing the status to the user.
After completing the operation, show the final result to the user.
Steps to create the application:
1. Create new android application project in eclipse with default settings.
2. Create the layout for the application.
3. Create theMainActivity and AsyncTaskRunner at com . example. asynctaskdemo . MainActivity . java
4. Run the application as given in the above screenshots.
3. Create the
4. Run the application as given in the above screenshots.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10pt" android:textColor="#444444" android:layout_alignParentLeft="true" android:layout_marginRight="9dip" android:layout_marginTop="20dip" android:layout_marginLeft="10dip" android:text="Sleep time in milliseconds:"/> <EditText android:id="@+id/et_time" android:layout_width="150dip" android:layout_height="wrap_content" android:background="@android:drawable/editbox_background" android:layout_toRightOf="@id/tv_time" android:layout_alignTop="@id/tv_time" android:inputType="text" /> <Button android:id="@+id/btn_do_it" android:layout_width="200dip" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginTop="15dip" android:layout_marginLeft="260dip" android:layout_below="@id/et_time" android:text="Run Async task" /> <TextView android:id="@+id/tv_result" android:layout_width="400dip" android:layout_height="100dip" android:textSize="7pt" android:layout_alignParentLeft="true" android:layout_below="@id/btn_do_it" android:layout_marginRight="9dip" android:layout_marginTop="15dip" android:layout_marginLeft="260dip" android:textColor="#AA0000" android:text=""/> </RelativeLayout>
MainActivity.java
package com example . . ; import android asynctaskdemo app . Activity; import android . . os AsyncTask; import android . . os Bundle; import android . view . Menu; import android . view . View; import android . widget . Button; import android . widget . EditText; import android . widget . TextView; /** * @author Prabu * AsyncTask . * */ public class exmple extends Activity MainActivity private { ; private EditText time; private TextView Button button ; @Override protected void onCreate finalResult Bundle savedInstanceState) ( super { . onCreate savedInstanceState); setContentView ( R ( layout . activity_main); time = (EditText) findViewById . R ( id . et_time); button = (Button) findViewById . R ( id . btn_do_it); . = (TextView) findViewById(R.id.tv_result); button finalResult . setOnClickListener new View ( OnClickListener . ) ( @Override public void onClick { View v) ( AsyncTaskRunner runner = new AsyncTaskRunner { ); String ( = time sleepTime . getText ) ( toString(); runner.execute(sleepTime); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } /** * @author Prabu * Private class which runs the long operation. . Sleeping for some time ( */ private class AsyncTaskRunner extends AsyncTask<String, String, String> ) private String { ; @Override protected String doInBackground resp String... ( ) params { publishProgress "Sleeping..."); // Calls ( onProgressUpdate ) try ( // Do your long operations here and return the result { time = Integer int parseInt . params ( 0]); // Sleeping for given time period Thread [ sleep . time); ( = "Slept for " + time + " milliseconds"; resp catch (InterruptedException e) } { e. printStackTrace ); ( = resp e. getMessage ); ( catch (Exception e) } { e. printStackTrace ); ( = resp e. getMessage ); ( return } ; resp /* * (non-Javadoc) * * @see android } . os AsyncTask#onPostExecute . ( java . lang Object) */ @Override protected void onPostExecute . String result) ( // execution of result of Long time consuming operation { finalResult . setText result); ( /* * (non-Javadoc) * * @see android } . os AsyncTask#onPreExecute . ) */ @Override protected void ( onPreExecute ) ( // Things to be done before execution of long running operation. For // example showing ProgessDialog { /* * (non-Javadoc) * * @see android } . os AsyncTask#onProgressUpdate . Progress ( ]) */ @Override protected void [ onProgressUpdate String... ( ) text { finalResult . setText text ( 0]); // Things to be done while execution of long running operation is in // progress. For [ ProgessDialog example updating } } }
AsyncTask <Void, Void, Void> task = new AsyncTask<Void, Void, Void>(){ @Override protected Void doInBackground( Void... params) { // Put in code over here that does the network related operations. return null; } }; task.execute();
No comments:
Post a Comment