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. About camera overlay

Deal all ~~~

I want to implement the camera preview with v4l2 overlay ~ Does anybody
knows If i need to use the function in the ovelay.cpp ? And who can tell
me what is the use of overlays.cpp in the path :

frameworks/base/libs/surfaceflinger/tests/overlays/

Thanks a lot ~~~


--~--~---------~--~----~------------~-------~--~----~

2. Getting a non-stretched Camera Preview Screen

Hi all,

I am trying to make an activity that (among other things) shows a
camera preview screen.  I have a translucent ImageView on top of the
camera preview, which is unstrechted.  The height is 295, so the width
should be ~400 pixels.  Unfortunately, no matter what I seem to do, I
cannot get the Preview I have (which is almost identical to the
apidemo camera preview View) to maintain a 4:3 view.  Instead, it is a
480x295 view, which produces undesirable horizontal stretches in the
preview.  None of my specifications of fixed sizes in the
SufraceHolder or the setPreviewSize() of the camera object make any
difference whatsoever.  I just want the camera preview to be 400x295,
with 40px margins on the left and the right of the screen so that it
is centered in teh same place as the image view.

Right now, I am stretching out the ImageView to the full screen so
they at least match, but ideally I would like to have vertical black
bars on the left and right edges of the screen to get more accurate
images.
--~--~---------~--~----~------------~-------~--~----~

3. G2 Devices

4. How to use SimpleCursorAdapter to include Button

5. Ticker and Merquee

6. How to assign/get the ip address of an emulator ?

7. HW acceleration of OpenGL on Android x86