Skip to main content

Android Background processing with AsyncTask

I had to download data from server and bind it with the ListView.I studied about this and finally used AsyncTask for background processing.I'm going to post some code and it's description about Android background processing.

AsyncTask

AsyncTask is designed to perform background processing and publish result on the UI thread.It is a helper class around Thread and Handler and does not constitute a generic threading framework.AsyncTask is defined by three generic types called Params,Progress and Result.And it has four callback methods called onPreExecute(),onPostExecute(),doInBackground() and onProgressUpdate().



public Load extends AsyncTask<Params,Progress,Result>
{
}



AsyncTask's generic types

Params:-It's a parameters send to the task upon execution.
Progress:-This parameter shows progress units during background processing.
Result:-It return type of result of the background processing.
If you don't require it's type then you can just use Void instead of it's generic types. 




public Load extends AsyncTask<Void,Void,Void>
{
}



AsyncTask's Callback methods



onPreExecute():-It invoke on the UI thread before the task execution.Generally we show ProgressDialog here if we want to show progress Dialog till background processing is completed.
onPostExecute():-It invoke on the UI thread after the task execution.Generally we dismiss ProgressDialog here if we want to dismiss Dialog when background processing is completed.
doInBackground():-It invoke on the background thread immediately after onPreExecute() execute.We put here code that we have to execute on the background thread like image downloading etc.
onProgressUpdate():-It invoke on the UI thread. This method is used to display any form of progress in the user interface while the background processing is still executing.
 We can also cancel task by calling cancel(true).


Example of Android background processing with AsyncTask.


public class Load extends AsyncTask<Void, Void, Void> {
    Context context;
    ProgressDialog progressDialog;

    public LoadLatestLive(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        /*put here code that you have to execete in the background thread*/
        return null;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        progressDialog = ProgressDialog.show(context, "", "Loading");
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        progressDialog.dismiss();
    }
}
 
 


Comments

Popular posts from this blog

Android O New Features Overview

This post assumes, you are an experienced developer who wants to get started with the latest version of Android. Android O is not a huge update to the OS and application framework like M but it still has many useful features for both developer and an user. Android O has focus on below areas. Notification redesigned Picture-in-Picture(PIP)  Visual adaption for different devices  Battery life improved Setting app reorganized Notification redesigned: Android O notification changes includes more easy and manageable way to manager your notification behavior and settings. It includes: Notification Channel: Notification channel allows you to create user customizable channel for each type of notification you wanna display. A single application can have multiple channel, a separate channel for each type of notification you wanna display. Having said this, you can create s separate channel for audio & image notification. User can disable specific notification channe...

Display News and Videos through RSS and Display it in Listview

Hi friends , In this tutorial ,i'm going to describe about how to fetch data from rss feed.Here i had also problem ,when i was developing my recent project.I had also faced  many problem and at last i developed my best project. Here is the code that describe how to fetch news headline from rss This class describes how to parse XML data: import java.net.URL; import java.util.ArrayList; import org.xmlpull.v1.XmlPullParser; import android.content.res.XmlResourceParser; import android.util.Log; import android.util.Xml; public class XmlFeedParser {         //Feed Parsing Method     public ArrayList<Implement> parse(String url) {                 //Array of Episode Objects         ArrayList<Implement> episodes = null;          ...

Display video from specific folder with rounded shape thumbnail

Hi friends ,This is complete post, about how to display video form specific folder and display it with rounded shape and play it on click event. For Rounded shape I have used custom ImageView. Now have a look on code part. main.xml <?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/gridView1"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:columnWidth="100dp"     android:gravity="center"     android:numColumns="auto_fit"     android:stretchMode="columnWidth" > </GridView> gridlayout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="wrap_content"     android:layo...