I am working in an Android application and I want to call AsyncTask from the main UI thread. For this, I want to call my AsyncTask from the stream.
This is the method that I call from my main UI thread. It works correctly
CommonAysnk mobjCommonAysnk = new CommonAysnk(this, 1); mobjCommonAysnk.execute();
CommonAysnk is my AsyncTask class. I want to pass my activity and integer parameter to the AsyncTask constructor. How can I call this from a stream as shown below.
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 the stream, and I can not pass the activity parameter correctly.
This is the CommonAysnk class. Please study 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 do this. Thanks
source share