Friday, February 7, 2014

A custom incorporated ProgressDialog and AsyncTask with abstract call back for result and exection

ok,,, Since i am little bit low on readers and subscribers i haven't got any comments on zombie apocalypse caused by the usage of an abstract class to wrap things. So am planning to wrap a ProgressDialog and AsyncTask inside an abstract class this time.
The reason behind this one is quite simple, i was working on the usual android stuff and got tired of implementing and writing progress bar loader each time when i use a network connection or some other asynchronous task. Please do share your thoughts about this one.

Anyhow, during execution, it will show a ProgressDialog window and a callback is made to onPreExecution() and awaits the task in doInAyncTask() to complete, then window will be hidden and a callback to  onPostExecution() is made. 



Usage (Inside an activity) : 

ProgressAsync progressWindow=new ProgressAsync(this,"Loading...") {

 public void onPreExecution(Context cxt) {
 // TODO Auto-generated method stub
 Log.e("Test.Dummy",  "inside onPreExecution");
 }
   
   
     @Override
     public String doInAyncTask(Object... pd) {
     // TODO Auto-generated method stub
      
 //code Some network connection or other aync functions as you require
    
 Log.e("Test.Dummy",  "inside doInAyncTask");

     return null;
     } 
   
   
 @Override
     public void onPostExecution(Object result,Context cxt) {
     // TODO Auto-generated method stub
    
 /* 
  runOnUiThread(new Runnable() {
        
      @Override
      public void run() {
      Toast.makeText(getApplicationContext(), "Task Complete !", Toast.LENGTH_SHORT).show();
      }
      });
 */

         Log.e("Test.Dummy",  "inside onPostExecution");
      }
      
     };
      
     progressWindow.execute(null);




Class : ProgressAsync.java

package com.example.android;

import android.app.ProgressDialog;
import android.content.Context;


public abstract class  ProgressAsync extends AsyncTask<Object, Void, Object>
 {
 ProgressDialog pd;
 Context context;
 
 public ProgressWindow(Context cxt,String msg)
 {
  context=cxt;
   pd=ProgressDialog.show(cxt,"",msg,true);
 }
 
 
 
 @Override
 protected void onPreExecute() {

  super.onPreExecute();
  onPreExecution(context);
 }
 
 
  @Override
  protected Object doInBackground(Object... pd) {
   return doInAyncTask(pd);
  }
  
  
  
  @Override
  protected void onPostExecute(Object result) {
   // TODO Auto-generated method stub
   
   pd.dismiss();
   
   super.onPostExecute(result);
   onPostExecution(result,context);
   
  }
  
  
 public abstract void onPreExecution(Context context);
 public abstract void onPostExecution(Object result,Context context);
 public abstract String doInAyncTask(Object... pd);
  
}

Followers