This AsyncTask class perform background operations and update the results on the UI
AsyncTask has three generic type
AsyncTask has three generic type
- 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
AsyncTask goes through 4 steps
- onPreExecute(),invoked on the UI thread immediately after the task is executed.
- doInBackground(Paaram ...),invoked on the background thread immediately after onPreExecute()finishes executing.
- onProgressUpdate(Progress...) invoked on the UI thread after a call to publishProgress(Progress...).
- onPostExecute(Result), invoked on the UI thread after the background computation finishes.
the source code is given below, please have a look. make a project AsyncTask and have a MainActivity with name MainActivity.java, the code is given below.
MainACtivity.java
package com.jitesh.asynctask;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btn_start;
ProgressBar progressBar;
TextView txt_percentage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_start = (Button) findViewById(R.id.btn_start);
progressBar = (ProgressBar) findViewById(R.id.progress);
txt_percentage= (TextView) findViewById(R.id.txt_percentage);
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn_start.setEnabled(false);
new ShowDialogAsyncTask().execute();
}
});
}
private class ShowDialogAsyncTask extends AsyncTask<Void, Integer, Void>{
int progress_status;
@Override
protected void onPreExecute() {
// update the UI immediately after the task is executed
super.onPreExecute();
Toast.makeText(MainActivity.this,
"Invoke onPreExecute()", Toast.LENGTH_SHORT).show();
progress_status = 0;
txt_percentage.setText("downloading 0%");
}
@Override
protected Void doInBackground(Void... params) {
//write the code which you do in background like downloading or using so internet resources etc
while(progress_status<100){
progress_status += 10;
publishProgress(progress_status);
SystemClock.sleep(300);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
txt_percentage.setText("downloading " +values[0]+"%");
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Toast.makeText(MainActivity.this,
"Invoke onPostExecute()", Toast.LENGTH_SHORT).show();
txt_percentage.setText("download complete");
btn_start.setEnabled(true);
}
}
}
/res/layout/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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="@dimen/padding_medium"
android:text="@string/async_task"
tools:context=".AsyncTaskActivity" />
<ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="34dp" />
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progress"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:minWidth="120dp"
android:text="@string/start_btn" />
<TextView
android:id="@+id/txt_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/progress"
android:text="downloading 0%"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
res/values/strings
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AsyncTask</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="start_btn">Start Button</string>
<string name="async_task">Async Task</string>
</resources>
res/values/dimens
<resources>
<dimen name="padding_small">8dp</dimen>
<dimen name="padding_medium">8dp</dimen>
<dimen name="padding_large">16dp</dimen>
</resources>
No comments:
Post a Comment