我认为您可以制作自己的 onTaskDone 界面,如下所示:
public class TaskTest extends AsyncTask<String, Integer, String> {
interface TaskDoneListener {
abstract void onTaskDone(String result);
}
private TaskDoneListener t;
public TaskTest(TaskDoneListener t){
this.t = t;
}
@Override
protected String doInBackground(String... params) {
// do your stuf
return "some thing";
}
@Override
protected void onPostExecute(String result) {
t.onTaskDone(result);
super.onPostExecute(result);
}
}
您可以使用 notify 和 wait 函数并使用 onTaskDone 通知某种正在等待结果的线程。
编辑:
在 onTaskDone 方法中执行此代码:
countryCode = mydb.getLatestCode();
Log.e("debug", countryCode);
mydb.close();
编辑2:
活动示例代码:
public class TestActivity extends Activity implements TaskDoneListener {
/** Called when the activity is first created. */
TaskTest task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//create your layout and stuff
}
@Override
protected void onStart() {
super.onStart();
//Dont start you task in the onCreate this could cause some weird behavior
//if the onCreate method is not yet done but your task is.
task = new TaskTest((TaskDoneListener) this, ##country_code?##, ##image?##);
task.execute();
}
public void onTaskDone(String result) {
//set the results in your created views
}
}
罗尔夫