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

jjwang

-- 

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

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

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

http://stackoverflow.com/questions/2739971/overlay-two-images-in-android-to-set-an-imageview

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

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(R.drawable.tt);
  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).

Thanks!

-- 

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?