• Have Any Question?
  • contact@eitworld.com

Run Long Tasks in Background





    • While creating your application, there are some tasks that take longer time than normal to be completed. During this time, your application becomes response less. It feels very much irritating to the user. Every developer wants that his application should never be like ‘Not Responding’. To solve the problem you need to run the task in background that will allow your application to keep responding. You may also show a progress dialog box asking the user to wait for completion of the task.
    • AsyncTask that stands for ‘asynchronous task’ is a class who allows a task to run in background thread and shows the result in UI thread.
    • Step 1- Make a separate class in program which will extend the class ‘AsyncTask’.

 

class MyTask extends AsyncTask<Params, Progress, Result>

{}

 

    • It has three generic types-

 

      • Params- This is the type of parameters which are sent to the task.
      • Progress- This is the type of progress units.
      • Result- This is the type of result which will be obtained after execution of task.
    • We can keep their values as Void.

 

    • Step 2- There are three important methods or we can say steps in AsyncTask-

 

      • onPreExecute()
      • doInBackground() [Mandatory]
      • onPostExecute()
    • As the name suggests, onPreExecute() contains the code that will be executed before execution of background task. doInBackground() contains the code for which we are doing the whole thing i.e. the task that we want to be performed in background. onPostExecute() is used to show the result of background task in UI thread.
@Override

protected void onPreExecute()

{

super.onPreExecute();

//code here

}

@Override

protected Void doInBackground(Void… arg0) {

// TODO Auto-generated method stub

return null;

}

@Override

protected void onPostExecute(Void result) {

// TODO Auto-generated method stub

super.onPostExecute(result);

}

 

    • Note:

 

      • doInBackground() method takes the ‘Params’ as its parameter. Thus this parameter must be of same type that we defined for Params while extending AsyncTask.
      • doInBackground() returns the ‘result’ of the task. It must be of same type that we defined for Result while extending AsyncTask.
      • onPostExecute() also takes the ‘result’ as its parameter.
    • In our case, the type of them is ‘Void’.

 

    • Step 3- This will work for you perfectly. Now if you want to show a ProgressDialog that may ask the user to wait while the background task is completed, you can create a variable for ProgressDialog in the class MyTask and start it in onPreExecute() method. Don’t forget to dismiss it in onPostExecute() method.

 

class MyTask extends AsyncTask<Void, Void, Void>

{

Drawable draw;

@Override

protected void onPreExecute()

{

super.onPreExecute();

Dialog = new ProgressDialog(MainActivity.this);

Dialog.show();

}

@Override

protected Void doInBackground(Void… arg0) {

// TODO Auto-generated method stub

return null;

}

@Override

protected void onPostExecute(Void result) {

// TODO Auto-generated method stub

super.onPostExecute(result);

Dialog.dismiss();

}

}

    • Step 4- Never try to show something on UI thread in doInBackground() method because background operations can’t be directly shown in UI thread. For showing the result, always use ‘onPostExecute() method.

 



Real Time Web Analytics