יום שישי, 25 באפריל 2014

Android AsyncTask

The following is a sample of using Android AsyncTask .
 
Part of the main activity that's call to start or cancel the AsyncTask .

 final Button TestAsyncTaskBtn = (Button) findViewById(R.id.TestAsyncTask);
        
        TestAsyncTaskBtn.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	
	        	
	        	mTheAsyncTask = new PlayWithAsyncTask(MainActivity.this);
	        	
	        	mTheAsyncTask.execute("Zvika" , "MS");	        	
	        	
	        }
        });
        
    final Button CancelAsyncTaskBtn = (Button) findViewById(R.id.CancelTestAsyncTask);
    
    
    CancelAsyncTaskBtn.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        
	        	if ( mTheAsyncTask != null )
	        	{
	        		if ( mTheAsyncTask.isCancelled() == false )
	        		{
	        			mTheAsyncTask.cancel(true);
	        		}	
	        	}
	        	
	        }
        });

The AsyncTask extender


package com.example.testapis;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
public class PlayWithAsyncTask extends AsyncTask <String , String , String >{
	private int mCount = 0 ;
	private ProgressDialog mProgress;
	private Activity mCallingActivity ;
	public PlayWithAsyncTask(Activity pCallingActivity ) {
		super();
		mCallingActivity = pCallingActivity;
	}
	
	@Override
	  protected void onPreExecute()
	  {
		mProgress = new ProgressDialog (mCallingActivity);
		
		mProgress.setCancelable(true);
		
		mProgress.setTitle("Waiting for action!!!");
		mProgress.setMessage("Before start");
		mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		mProgress.setProgress(0);
		
		mProgress.setMax(30);
		
		mProgress.show();
		
	  }
	
	@Override
	protected String doInBackground(String... arg0) 
    {	
    	Log.i ("PlayWithAsyncTask" , "The data: " + arg0[0]);
    	
		while (  mCount < 30 )
		{
		
			try {
				Thread.sleep(100);
				publishProgress ( Integer.toString(mCount));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.i ("PlayWithAsyncTask" , e.toString());
			}
			
			mCount ++;
		}
		
		return "Count until:" + Integer.toString(mCount);
	}
	@Override
	 protected void onPostExecute(String pResult )
	 {
		Log.w("PlayWithAsyncTask ",  " The execute result: " + pResult);
		
		mProgress.dismiss();
	 }
	
	@Override
	protected void onCancelled() 
	{
		Log.i ("PlayWithAsyncTask" , "onCancelled was called ");
	
		mProgress.dismiss();
	}
	@Override
	protected void onProgressUpdate(String... pvalues)				   	
	{
		mProgress.setMessage("" + pvalues[0]);
		
		mProgress.incrementProgressBy(1);
		
		Log.w("PlayWithAsyncTask",  " - the Progress - " + pvalues[0]);
	}
	
}

The result 
Capture2



Note :
The AsyncTask template contains 3 type parameters :
Params, the type of the parameters sent to the task upon execution.
Progress, the type of the progress units published during the background computation.
Result, the type of the result of the background computation.

from http://developer.android.com/reference/android/os/AsyncTask.html

אין תגובות:

הוסף רשומת תגובה