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();
    }
}
 
 


Popular posts from this blog

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:layout_height="wrap_content"
    android:orientation="verti…

Custom Volley-Gson request implementation

This page covers Volley-Gson integration and also Volley custom Request. We assume that, you are familiar with Volley and Gson.

Volley has some inbuilt request which you can use, if your response is a String, Image, or JSON, in that case, you don't need to implement a custom Request.  To use Gson with Volley, we need to implement a custom request.
To make a custom request, you need to perform following operation.
Extend Request<T> class, where T is the type of parsed response or expected response. Implement the abstract methods parseNetworkResponse() and deliverResponse().  parseNetworkResponse:  A response contains parse response of a given type i.e Gson.

@Override
    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        try {
            String json = new String(
                    response.data, HttpHeaderParser.parseCharset(response.headers));
            if (BuildConfig.IS_DEBUG)
                Log.d(TAG, "Response :: " + …

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;

        try {
            //Encode the URL into a URL Object
            URL episode_feed_url = new URL(url);

            //Open a Connection to the feed
            XmlPullParser parser = Xml.newPullParser();
            try {
                parser.setInput(episode_feed_url.o…