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