1. If your code is on onPostExecute(), your code is already running in
the UI thread. Just call a method on your activity to execute that
chunk of code (instead of holding a reference to 'context', hold a
reference to your activity instead).

2. Just call this again:
      StudentTask st = new StudentTask(getThis());
      st.execute(new Object[] {StudentTask.Task.AUTH, null, userName,
password });

Tasks are like executable messages that will be executed on a
background thread. Creating multiple tasks does not necessarily create
multiple threads. Tasks are scheduled on the available background
threads. Whenever you create a new AsyncTask and call 'execute', the
task will be scheduled on the next available thread. The next
available thread is obtained from a pool of threads (I don't know how
large the pool is that AsyncTask uses).

On Jan 22, 5:21 am, yasirmturk <[email protected]> wrote:
> i have a class derived from AsyncTask and i initialize it in my
> activity. Now i m facing two problems...
>
> 1. I want to send result back to my activity and execute a chunk of
> code in my activty on onPostExecute(Object result) event. (i tried
> handler but its not working)
> 2. I want to execute asynctask multiple times.
>
> Please suggest me some better solution or a fix to this...
> thanx in advance
>
> ACTIVITY CODE:
>
>                          StudentTask st = new StudentTask(getThis());
>                          st.execute(new Object[] {StudentTask.Task.AUTH, 
> null, userName,
> password);
>
> CLASS CODE:
>
> public class StudentTask extends AsyncTask {
>
>         private Context context;
>         private ProgressDialog pd;
>
> public static enum Task {
>                 SYNCRONIZE, AUTH, ACTIVATE
>         }
>
> public StudentTask(Context context) {
>                 super();
>                 this.context = context;
>         }
>
>         @Override
>         protected void onPreExecute() {
>                 pd = ProgressDialog.show(context, "Please wait", "Starting");
>         }
>
>         @Override
>         protected Object doInBackground(Object... params) {
>
>                 Object result = null;
>                 Task t = (Task) params[0];
>                 switch (t) {
>                 case AUTH:
>                         if (this.authenticateStudent(params[1].toString(),
>                                         params[2].toString()).IsSuccessFul()) 
> {
>                                 result = true;
>                         } else {
>                                 result = false;
>                         }
>                         break;
>                 case SYNCRONIZE:
> result =                        this.synchronize(null, null);
>                         break;
>                 default:
>                         break;
>                 }
>                 return result;
>         }
>
>         @Override
>         protected void onProgressUpdate(Object... values) {
>                 pd.setMessage(values[0].toString());
>                 super.onProgressUpdate(values);
>         }
>
>         @Override
>         protected void onPostExecute(Object result) {
>                 pd.dismiss();
>                 // Send this result back to activity and execute some code 
> there
> e.g. launch next activity or display error
>                 super.onPostExecute(result);
>         }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to