What is Asynctask in android ?
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.
Methods Of Asynctask
doInBackground() :
This method contains the code which needs to be executed in background. In this method we can send results multiple times to the UI thread by publishProgress() method. To notify that the background processing has been completed we just need to use the return statements
onPreExecute() :
This method contains the code which is executed before the background processing starts
onPostExecute() :
This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method.
onProgressUpdate() :
This method receives progress updates from doInBackground method, which is published via publishProgress method, and this method can use this progress update to update the UI thread.
Example of AsyncTask
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.bhaumik.asynctaskdemo.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="AsyncTask Example" android:textColor="#000" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:textSize="18dp"/> <ProgressBar android:id="@+id/progress" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/Widget.AppCompat.ProgressBar.Horizontal" android:layout_marginTop="20dp"/> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:textColor="#0000FF" android:text="Hello" android:textSize="18dp"/> <Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Start Download" /> </LinearLayout>
MainActivity.java
package com.example.bhaumik.asynctaskdemo; import android.os.AsyncTask; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ProgressBar progressBar; TextView textView; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar = (ProgressBar) findViewById(R.id.progress); textView = (TextView) findViewById(R.id.textView); button = (Button) findViewById(R.id.btn_start); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { button.setEnabled(false); new MyTask().execute(); } }); } private class MyTask extends AsyncTask<Void,Integer,Void>{ int progress_status; @Override protected void onPreExecute() { super.onPreExecute(); progress_status = 0; Toast.makeText(MainActivity.this,"OnPreExecute() Called",Toast.LENGTH_SHORT).show(); textView.setText("Downloading 0%"); } @Override protected Void doInBackground(Void[] params) { while (progress_status<100) { progress_status +=5; publishProgress(progress_status); SystemClock.sleep(1000); } return null; } @Override protected void onProgressUpdate(Integer[] values) { super.onProgressUpdate(values); progressBar.setProgress(values[0]); textView.setText("Downloading " + values[0] + "%"); Toast.makeText(MainActivity.this,"onProgressUpdate() Called",Toast.LENGTH_SHORT).show(); } @Override protected void onPostExecute(Void o) { super.onPostExecute(o); Toast.makeText(MainActivity.this,"onPostExecute() Called",Toast.LENGTH_SHORT).show(); textView.setText("Download Complete"); button.setEnabled(true); } } }
0 comments:
Post a Comment