Threading in activity and AsyncTask problem

by yasirmturk » Sat, 23 Jan 2010 02:28:10 GMT


Sponsored Links
 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);
        }

--



Threading in activity and AsyncTask problem

by Streets Of Boston » Sat, 23 Jan 2010 02:49:02 GMT


 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).




--


Sponsored Links


Other Threads

1. cara ngilangin (hide) di galery

misi rekan2, ada yg tau cara hide file2 gambar yg ada di galery ga?
jadi gua pgn tuh di galery cuma utk wallpapper sama foto2 aja.

c/o : ada game yg nyimpen banyak gambar, nah kl buka galery penuh tuh
sama pic2 nya dia. ada cara hide nya ga yah?

saya pake moto droid 2.
thanks

-- 
===============

2. Rooted, whats next? WAS: pilih z4root or SuperOneClick utk Galaxy tab?

thanks ya atas respondnya... sekalian mau report nih
setelah gugling akhirnya tadi sore download z4root ke hh dan run
applikasinya;

run pertama pilih temporary root dan tidak kejadian apa2 alias HH biru
dengan tulisan harap tunggu selama proses root berjalan. tunggu 20 menit
akhirnya di bilang prosesnya gak bisa di complete

run kedua masih pake z4root n masih pilih temporary root hasilnya biru
dengan text yg sama sebentar lalu layar blank tp lampu menu dkk fi bawah
nyala.. tunggu 30 menit mulai was2 trus restart pake power+volume akhirnya
ngeboot ulang tanpa ada masalah (mudah2an)

run ketiga nyoba pake superoneclick lewat PC gak nyampe 3 menit proses
selesai dan ada superuser iconnya...

trus enaknya diapain lagi ya Gtabnya suhu2?
ditunggu comment dan sarannya nih

Riz

2011/2/8 Ignasius Kristiawan <kristdr...@gmail.com>




-- 
===============

3. Very OOT. Apakah HP Android juga spt ini? Palomitas_al_movil.wmv

4. How to provide Complex rendering support in android

5. Droid 2 Global

6. ndk-gdb on cygwin/windowsXP won't load symbol tables.

7. [OOT] knapa yah xl-axiata data hilang??