Android - Background Processing using AsynchTask Example


In this series of tutorials we will access data from remote database. We will receive JSON object from URL. Then We will decode the received JSON object and then display the useful data on android screen.

Steps Involved

  1. Create an Activity.
  2. Create a background task to handle network connection.
  3. Receive JSON object from URL.
  4. Decode message from URL.
  5. Display data.

Create a new Activity

public class MainActivity extends Activity {
}

Create an AsynchTask

Why we need background processing

Each android application runs on main thread. Main thread handles multiple tasks like handling of callback functions, updating user interface. All the application components like Activity, broadcast receiver, Services work on the same main thread. So those operations which blocks the main thread should be executed in separate thread.

How to create a separate thread

We can use background processing using AsynchTask. AsynchTask helps to perform background operations and it also facilitates updation of user interface.

Create a subclass of AsynchTask

class BackgroundProcessing extends AsyncTask<String, String, String>{
}

AsynchTask goes through certain steps.

  1. Implement what you want to do before background processing processing.
    	@Override
    		protected void onPreExecute() {
    			// TODO Auto-generated method stub
    			super.onPreExecute();
    		}
  2. Implement what you want to do in background.
    @Override
    		protected String doInBackground(String... params) {
    			// TODO Auto-generated method stub
    			return null;
    		}
  3. Write code for what to do after finshing background task.
    	@Override
    		protected void onPostExecute(String result) {
    			// TODO Auto-generated method stub
    			super.onPostExecute(result);
    		}

To execute the AsynchTask you need to add following code to your activity.

new BackgroundProcessing().execute();

Complete code so far

package com.example.jsonkb4dev;
import java.nio.channels.AsynchronousCloseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	class BackgroundProcessing extends AsyncTask<String, String, String>{

		

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			return null;
		}

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

}

Some points you need to know

  • The AynchTask must be called on main thread.
  • The task instance should be created on main thread.
  • This task can be executed once.
  • AsynchTask assures that are all methods are synchronized.
  • AsynchTask should be performed for few seconds.

So in this tutorial we learned how to take a step towards implementing json parsing from URL. We created an AsynchTask class. In the next tutorial we will create HTTP client to request a data from server.