How to timeout Asynctask and dismiss ProgressDialog?

六眼飞鱼酱① 提交于 2019-12-04 14:02:10

According to the Docs

Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.

instead of doing

if(timeout == true)
     {
         Log.d(TAG, "TimeOut = true");
         onPostExecute(null);  // this should be removed

just return null there instead. This will return control to onPostExecute() where you can close your Dialog.

Edit

I think you are making it too complicated with the TimerTask in this situation because everything will continue to run. What you can do is use a while loop and a counter for whatever time you want. So something like

long waitTime = 1000;  // or whatever you want the timeout length to be
long curWaitTime = 0;
while (!timeout && curWaitTime < waitTime)
{
    // put your network/file code here
    // if the data finishes then you can set timeout to true
    curWaitTime += 100; // or some other value to add
    Thread.sleep(100);
}
return null;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!