【发布时间】:2012-04-27 12:56:56
【问题描述】:
我已经写了如下代码
公共类 SplashScreen 扩展 BaseActivity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
new DownloadPlistTask().execute("background","Progress","yes");
}
private class DownloadPlistTask extends AsyncTask<String,String,String>{
protected String doInBackground(String... dummy){
return compareLocRemPlist();
}
protected void onProgressUpdate(String... progress){
//some code here.
}
protected void onPostExecute(String result){
//handle return type from doInBackground.
}
public String compareData(){
final Timer timer = new Timer();
timer.schedule( new TimerTask(){
public void run(){
//code regarding webservices.
if(boolValue){
//if the boolValue from the webservice is true,cancel the timer.
timer.cancel();
}
},5*60*1000,5*60*1000);
return "finish";
}
}
}
在上面的代码中,来自 onCreate() iam 调用 AsyncTask doInBackground 在 doInBackground iam 调用名为 compareData() 的方法。在 compareData() 方法中 iam 使用计时器。计时器在 webservice 中发送对 webservice 的请求我得到一个布尔值,如果它是真的我需要返回完成。如果它是假的我不想返回完成,我应该留在 compareData() 方法中,并且每五分钟它应该发送请求直到我得到是的。但是当计时器等待五分钟时,在此期间,计时器后的语句正在执行并且返回值完成返回到 doInBackground 并且控件将转到 onPostExecute() 但在后台计时器正在运行。当我取消计时器时如何返回完成
【问题讨论】: