我在一个 android 应用程序中工作,我想从我的 UI 主线程调用一个 AsyncTask。为此,我想从线程中调用我的 AsyncTask。
这是我从主 UI 线程调用的方法。这工作正常
CommonAysnk mobjCommonAysnk = new CommonAysnk(this, 1);
mobjCommonAysnk.execute();
CommonAysnk 是我的 AsyncTask 类。我想将我的活动和一个整数参数传递给 AsyncTask 构造函数。如何从线程中调用它,如下所示的方法。
Thread t = new Thread() {
public void run() {
try {
CommonAysnk mobjCommonAysnk = new CommonAysnk(this, 1);
mobjCommonAysnk.execute();
} catch (Exception ex) {
}}};
t.start();
当我尝试从线程调用它时,我无法正确传递活动参数。
这是 CommonAysnk 类。请调查一下
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;
}
}
我们怎么能做到这一点。谢谢