4

I am working in an android application and I want to call an AsyncTask from my UI main thread. For that I want to call my AsyncTask from a thread.

This is the method that I call from my main UI thread. This is working correctly

  CommonAysnk   mobjCommonAysnk = new CommonAysnk(this, 1);
  mobjCommonAysnk.execute();

CommonAysnk is my AsyncTask class.I want to pass my activity and an integer parameter to the AsyncTask constructor. How can I call this from a thread as shown below method.

 Thread t = new Thread() {
                public void run() {
                    try {                       
            CommonAysnk   mobjCommonAysnk = new CommonAysnk(this, 1);
            mobjCommonAysnk.execute();
                    } catch (Exception ex) {

                    }}};                            
            t.start();

When I tried to call it from a Thread and I am not able to pass the activity parameter correctly.

This is CommonAysnk class. Please look into it

public class CommonAysnk extends AsyncTask<URL, Integer, String> {

    private Common mobjCommon = null;
    private Activity mobjActivity = null;
    private int mcallIntentcond = 0;
    private ProgressDialog mProcessDialog = null;

    public CommonAysnk(Activity activity, int condition) {
        mobjActivity = activity;
        mcallIntentcond = condition;

    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        mProcessDialog.dismiss();
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mobjCommon = new Common();
        mProcessDialog = mobjCommon.showProgressDialog(mobjActivity, "",
                "Loading...", false);
    }
    @Override
    protected String doInBackground(URL... params) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {

        }
        switch (mcallIntentcond) {
        case 1:
            Intent i=new Intent(mobjActivity, Home.class);
            mobjActivity.startActivity(i);
            mobjActivity.finish();
            break;
        }

        return null;
    }
}

How can we sole this. Thanks

Arun PS
  • 4,610
  • 6
  • 41
  • 56
  • first thing that I notice is that you're starting activities withing your `doInBackground()`.. why don't you do it inside `onPostExecute()` ? also you're starting a thread and then calling the asycTask within a thread.. you really need to reconsider what you're doing!! – ColdFire Oct 03 '12 at 11:48
  • Please look in to my question A.A. I am trying to start my AsynkTask from another Thread.There are issued when we start AsynkTask from the UI main thread. Refer this link for more details http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception I think we have to to all the functions in doInBackground() in AsynkTask. Even if I call my Intent in onPostExecute() how can I solve my problem. – Arun PS Oct 03 '12 at 11:53

4 Answers4

3

There is no reason to run AsyncTask within a thread like this, you can call this on the UI thread. AsyncTask manages the threading for you.

The code you enter in the doInBackground method is automatically run in a background thread, the other methods on your AsyncTask are run on the UI thread and you can directly interact safely with the UI.

Philio
  • 3,675
  • 1
  • 23
  • 33
  • Hi Philio , Thanks for the reply. There are some problems when we run AsyncTask in UI main thread. The error is android.os.NetworkOnMainThreadException. Please look into the link for further information. http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Arun PS Oct 03 '12 at 11:45
  • To clarify what I mean is you can simply instantiate and run the AsyncTask from the UI thread, e.g. new SomeAsyncTask().run() rather than running it within a thread. – Philio Oct 12 '12 at 13:35
2

You cannot use this directly from inside a thread as the context changes from your MainActivity to the thread class. So you need to do the following,

CommonAysnk   mobjCommonAysnk = new CommonAysnk(ActivityName.this, 1); 

And you may run AsyncTask from within a thread, no hard and fast rules with regard to that.

Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
  • Hi Royston,Thanks for the reply. The constructor CommonAysnk(Class, int) is undefined. Still I get this error. Pleasesolve this – Arun PS Oct 03 '12 at 11:36
  • Ok, i get it, change to CommonAysnk mobjCommonAysnk = new CommonAysnk(); – Royston Pinto Oct 03 '12 at 11:37
  • Then how can I pass the parameter values to the CommonAysnk class constructor. – Arun PS Oct 03 '12 at 11:39
  • You can, remove "this" as you need not pass the context, just pass the number "1" – Royston Pinto Oct 03 '12 at 11:41
  • Please look into my question. I have updated with my CommonAysnk class. – Arun PS Oct 03 '12 at 11:44
  • Ok, so firstly, u need not pass the Activity to AsyncTask. So just pass the Integer. Secondly, u dont need a constructor, your arguments passed can be acessed, request you to google the same. Thirdly, in switch case, instead of returning null, return a valid string. You can use this String in onPostExecute() to launch you Intent. Since onPostExecute() runs on UI thread u can use "this" there without needing a context. Hope this helps! – Royston Pinto Oct 03 '12 at 13:43
1

use

 CommonAysnk mobjCommonAysnk = new CommonAysnk(ClassName.this, 1);
 mobjCommonAysnk.execute();
ColdFire
  • 6,764
  • 6
  • 35
  • 51
1

It is mandatory to call asynctask only from main thread, else it may crash at run time when we try to touch UI from onPreExecute or onProgressUpdate or onPostExecute functions.

SNce
  • 2,453
  • 1
  • 16
  • 14