AsyncTask In Android Examples || Background Process

In this tutorial we will learn how to use AsyncTask class in Android. Running the task in background and updating the UI part of in android using AsyncTask.

AsyncTask In Android
AsyncTask In Android FlutterTpoint

What is AsyncTask In Android?

AsyncTask is class which used to runs the task in background. So we can use this class to perform the task in background and update the UI after completing the task.

Generally it has three methods which are below:

  • onPreExecute()
  • onPostExecute()
  • doInBackground()

->onPreExecute() AsyncTask Method in Android

Before doing background operation we can show something like progress bar on the screen.

->doInBackground() AsyncTask Method

In this method we will do background task on the background thread.

->onPostExecute() AsyncTask Method

We will get result in this method and will update the UI thread.

Complete Class Example

We have to create a class and extended it the AsyncTask class. In the following example we created two classes , One is TestActivity and another is AsyncTaskExample. The AsyncTask method is called from the TestActivity class.

package com.example.androidtestingproject.Activity;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.androidtestingproject.R;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class TestActivity extends AppCompatActivity {
    URL ImageUrl = null;
    InputStream is = null;
    Bitmap bmImg = null;
    ImageView imageView = null;
    Button button;
    ProgressDialog p;
    AsyncTaskExample asyncTask = new AsyncTaskExample();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        button = findViewById(R.id.button);
        imageView = findViewById(R.id.imageView);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                asyncTask.execute("https://www.fluttertpoint.com/wp-content/uploads/2021/09/cropped-FlutterTPoint-Logo-3-220x54.png");
            }
        });
    }

    private class AsyncTaskExample extends AsyncTask<String, String, Bitmap> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            p = new ProgressDialog(TestActivity.this);
            p.setMessage("Please wait...It is downloading");
            p.setIndeterminate(false);
            p.setCancelable(true);
            p.show();
        }

        @Override
        protected Bitmap doInBackground(String... strings) {
            try {
                ImageUrl = new URL(strings[0]);
                HttpURLConnection conn = (HttpURLConnection) ImageUrl.openConnection();
                conn.setDoInput(true);
                conn.connect();
                is = conn.getInputStream();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.RGB_565;
                bmImg = BitmapFactory.decodeStream(is, null, options);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bmImg;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            if (imageView != null) {
                p.hide();
                imageView.setImageBitmap(bitmap);
            } else {
                p.show();
            }
        }
    }
}

Result

When you click on the download image button. The progress bar appears. The progress bar disappears after the progress finishes.

AsyncTask Scteenshot
AsyncTask Scteenshot

In this above process, The link is passed to the onPreExecute() method. The doInBackground() fetches the image from server using the URL.

Thank you for visiting this tutorial on FlutterTpoint. Hope you learn AsyncTask from here. Please learn more android tutorial which will help you to improve your skills as android developer from here.

close

Don’t miss new tips!

We don’t spam! Read our [link]privacy policy[/link] for more info.

Leave a Comment