如何设置AsyncTask的计数器?

时间:2011-09-07 21:00:52

标签: android counter

我有一个从互联网上检索信息的AsyncTask。但是当信号或wifi连接很弱时。 AsyncTask超时,应用程序强制关闭。

我想也许我可以设置一个计时器,所以当AsyncTask超过20秒时,它会显示一个对话框甚至是一个toast,并将用户发送到该类中的上一个活动。

我将如何做到这一点?

我知道聪明的方法是计算增加一个int

也许像..

private class fetcher extends AsyncTask<Void,Void,Void>{

protected void DoInBackground(){


for(int i = 0;i<20;i++){


//DO something

我是在正确的轨道上吗? 一些指导会有所帮助!

1 个答案:

答案 0 :(得分:2)

你可以简单地做这样的事情

private class fetcher extends AsyncTask<Void,Void,Void>{

protected void DoInBackground(){
   try{
   //do your stuff here
   }catch(Exception e){
   //something is wrong , async task may crash , show a dialog or error
}

}