如何超时Asynctask并解除ProgressDialog?

Har*_* Un 3 android timeout progressdialog android-asynctask

我有一个问题,希望你能帮助我.我有一个Asynctask,如果我按下mainactivity中的按钮,它会开始上传数据.它工作正常,除非我有一个缓慢的互联网连接.Asynctask启动了progressdialog,如果我连接速度慢,Asynctask会停止,但Progressdialog不会消失,因为它从未到达onPostExecute.

现在我正在尝试实现超时但无法找到方法,因此progressdialog不会这样做.

这是我的代码:

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        time = System.currentTimeMillis();

        Log.d(TAG, "PBar: Hat gestartet");
        if (MainActivity.MessungStart == true)
            {
        ConnectionTask.dialog = new ProgressDialog(MainActivity.context);

        ConnectionTask.dialog.setMessage("Daten werden hochgeladen...");
        dialog.setCanceledOnTouchOutside(false);
        ConnectionTask.dialog.show();    
            }
    }

protected Void doInBackground(String... args) {

        mUser = MainActivity.username; 
        mPassword = MainActivity.password;  
        Log.d(TAG,"Async: User= "+mUser+" Password= "+mPassword);

        Timer t = new Timer();
        TimerTask tk = new TimerTask() {

              @Override
                public void run() {
                    timeout = true;
                }
            };
         t.schedule(tk, 100);
         if(timeout == true)
         {
             Log.d(TAG, "TimeOut = true");
             onPostExecute(null);
         }

        try {
            authenticate();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        {
            try {
                sendData();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if(temp!= null)
        {
        try {
            ReceiveMessageState();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
        Sendungfertig = true;   
        if(MainActivity.asyncIsCanceled == true)
        {
            if (ConnectionTask.dialog.isShowing() == true) 
            {
             ConnectionTask.dialog.dismiss();
            }
        }
        isCancelled();
        return null;
   }

@Override
protected void onPostExecute(Void v) {
    super.onPostExecute(v);
    Log.d(TAG, "PBar: Sollte enden");
    if(MessageChecked==true)
    {
    if (ConnectionTask.dialog.isShowing() == true) 
    {
     ConnectionTask.dialog.dismiss();
    }


    if(isCancelled()==true)
    {
    if (ConnectionTask.dialog.isShowing() == true) 
    {
     ConnectionTask.dialog.dismiss();
    }
    MessageChecked = false;
    MainActivity.MessungStart = false;
    }
    }
}
Run Code Online (Sandbox Code Playgroud)

cod*_*gic 6

根据文件

不要手动调用onPreExecute(),onPostExecute(Result),doInBackground(Params ...),onProgressUpdate(Progress ...).

而不是做

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

return null在那里.这会将控制权返回给onPostExecute()你可以关闭你的地方Dialog.

编辑

我认为你TimerTask在这种情况下使它太复杂了,因为一切都将继续运行.你能做的就是while loop随时随地使用一个和一个计数器.所以像

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;
Run Code Online (Sandbox Code Playgroud)