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 channel instead

Java 8 Overview

Java 1.8 has introduced major features for the Java developers. It includes various upgrade to the Java programming, JVM, Tools and libraries. The main purpose of Java 1.8 release has been to simplify programming, utilize functional programming benefits and enable parallel programming/processing in Java programming language. Java 1.8 feature  Lambda(->) Expression Functional interfaces Default Methods in interface static method inside interface     Pre defined functional interfaces Predicate Function Consumer ::(Method reference and constructor reference by using double colon(::)operator) Stream API Date & Time API

Overview of how to develop native code with Android NDK:

  Create a jni directory and place native source code under $PROJECT/jni/... Create Android.mk directory and place it under $PROJECT/jni/... to describe source code to the NDK build system. Create Application.mk and place it under $PROJECT/jni/...to describe project in more details to the NDK build system.It is an optional. Finally need to build native source code by running '$NDK/ndk-build' from project directory or it's sub-directory. Android.mk An Android.mk file is a small build script that we write to describe sources to the NDK build system. It's syntax is like this... LOCAL_PATH:=$(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE:=hello-jni LOCAL_SRC_FILES:=hello-jni.c include $(BUILD_SHARED_LIBRARY) NDK groups your sources into "modules", where each module can be one of the following: Static library Shared library We can write several module in a single 'Android.mk' or can write seve