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


                         StudentTask st = new StudentTask(getThis());
                         st.execute(new Object[] {StudentTask.Task.AUTH, null, 


public class StudentTask extends AsyncTask {

        private Context context;
        private ProgressDialog pd;

public static enum Task {

public StudentTask(Context context) {
                this.context = context;

        protected void onPreExecute() {
                pd =, "Please wait", "Starting");

        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;
                case SYNCRONIZE:
result =                        this.synchronize(null, null);
                return result;

        protected void onProgressUpdate(Object... values) {

        protected void onPostExecute(Object result) {
                // Send this result back to activity and execute some code there
e.g. launch next activity or display error


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. why doesn't google provide us a pure C/C++ SDK for android?

First of all, I know the android ndk, but it's not a full functional C/
C++ sdk for android platform.

For example It doesn't support the standard ui components

Can you guys tell me why and what am I supposed to do to access
standard ui controls in pure C/C++ codes.

Thanks a lot   :-)



2. Dynamically build status bar icon from layers in Java, but can't figure-out resource ID

I would like to dynamically build-up a status bar icon from multiple

Currently, the only way I can do this is using a ton of layer-list XML
files for all the combinations of the icons I would like to display.

I've seen code for doing something similar to the layer-list.xml files
in Java, but I can't figure-out how to "assign" my new layerlist or
layerdrawable to my icon's resource ID (required with the notification

There was something very close to what I want to do over on
StackOverflow in "solution #2 (dynamic)" of this post:

Daniel's "solution #1" (in the above link) works wonderfully for the
static .xml files, but I'm looking for a more elegant, dynamic

In the above post, the code snippet:

  Resources r = getResources();
  Drawable[] layers = new Drawable[2];
  layers[0] = r.getDrawable(R.drawable.t);
  layers[1] = r.getDrawable(;
  LayerDrawable layerDrawable = new LayerDrawable(layers);

appears to be very close to what I want, but I don't know or
understand how to "assign" the new layerDrawable to my notification
icon (which takes an integer resource ID).

I have a sneaky suspicion that I simply cannot do this...but I thought
I'd ask here (its been over a month on StackOverflow with no replies).



3. Problem with audio recording

4. Camera Application Scene Mode Setting

5. wheel View

6. Working with offline maps based on Open Street Maps

7. Any on-device logcat viewer capable of filtering by package name?