Facebook

Home » , , , » How to do background processing in android

How to do background processing in android

Written By Martin Kariuki on Friday, 27 May 2016 | 07:52

One of the goals of android is to create a rich user experience and minimize a situation where the user has to wait while the device hangs a while processing. Infact, android will kill that process before completion and inform you that the app is not responding.
Programmatically, new releases of android have come up with rules that prevent you from doing a resource intensive activity on the UI thread. As a programmer with good knowledge of Java Threading, you can roll out a full background processing activity using Threading in java. However, android has a way of creating background processing activity using the AsyncTask class.
This class has to define three parameters:
1.       The input parameter to be passed to the execute method,
2.       The Integer parameter to update the progress indicator,
3.       The output parameter to be passed out after the process has completed.
Your class must implement at least one method of the AsyncTask object called doInDackGround.
Other methods that can be implemented are:
onPreExecute() –used to do some initialization before starting to process
onPostExecute()-used to update the User interface after processing has completed
onProgressUpdate()-used to update the progress indicator.
To get our hands dirty let us create a class called AsyncCounter that is going to count upto 1000000 in the background.
The skeleton of the class looks like this:
you declare the class extending AsyncTask as an inner class within the Activity or Fragment where you want to use it.
You then override the parent method doInBackground using the annotation @Override and  providing your own implementation.
If you look at our blue print, AsyncTask<Integer,Integer,Integer> you see three integer types. As we indicated above the first parameter will be passed to the doInBackground(Integer... params) method. The three dots (…) mean that you can pass more than one parameter. To access the first parameter within the doInBackground(Integer... params) method, use a zero based index like this
@Override
 protected Integer doInBackground(Integer... params) {
int parameter1 = params[0];       
return null;
    }
we want to pass the upper limit for our loop and we want the method to return the highest number after completing the loop in the background. So the full implementation looks like this.
 
After now we are through with the processing, we can update our UI with the value using the onPostExecute()

@Override
protected void onPostExecute(Integer integer){


//label1 is a TextView in activity_main.xml


label1.setText(String.valueOf(integer));


}


To use this class you create a new instance and call the execute() method. Remember to pass the input parameter.

btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//start processing when a button is clicked
new AsycCounter().execute(10000000);
}
});


0 comments :

Post a Comment

Recent