Reputation: 385
i have written the code like below
public class SplashScreen extends 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";
}
}
}
In the above code from the onCreate() iam calling AsyncTask doInBackground in the doInBackground iam calling a method named compareData().In the compareData() method iam using a timer.The timer sends request for the webservice,in the webservice iam getting a boolean value,if it is true i need to return finish.If it is false i dont want to return finish,i should stay in the compareData() method,and for every five minutes it should send the request till i get true.But when the timer is waiting for the five minutes,during this time the statements after the timer is getting exetuted and the return value finish is returning to the doInBackground and the control is going to the onPostExecute() but in the background the timer is running.how can return finish when i cancel the timer
Upvotes: 1
Views: 1682
Reputation: 15774
Since you are running it in doInBackground
, you can perform the check synchronously. So instead of a timer, you can make use of the Thread.sleep
method.
static final long RETRY_DURATION = 5*60*1000;
public String compareData(){
boolean boolValue = false;
do{
//code regarding webservices.
if(!boolValue){
try{
Thread.sleep(RETRY_DURATION);
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
}while(!boolValue);
return "finish";
}
Upvotes: 2