Android 如何超时异步任务并关闭ProgressDialog?

Android 如何超时异步任务并关闭ProgressDialog?,android,android-asynctask,timeout,progressdialog,Android,Android Asynctask,Timeout,Progressdialog,我有个问题,希望你能帮助我。 我有一个Asynctask,如果我按下mainactivity中的一个按钮,它就会开始上传数据。 它工作正常,除非我的互联网连接很慢。 Asynctask启动一个progressdialog,如果连接速度慢,Asynctask会停止,但progressdialog不会消失,因为它从未到达onPostExecute 现在,我试图实现一个超时,但找不到方法,因此progressdialog将关闭以执行此操作 这是我的密码: @Override prote

我有个问题,希望你能帮助我。 我有一个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;
    }
    }
}

不要手动调用onPreExecute()、onPostExecute(结果)、doInBackground(参数…)和onProgressUpdate(进度…)

而不是做

if(timeout == true)
     {
         Log.d(TAG, "TimeOut = true");
         onPostExecute(null);  // this should be removed
只需在那里返回null即可。这将使控件返回到
onPostExecute()

编辑

我认为在这种情况下,您使
TimerTask
变得过于复杂,因为一切都将继续运行。您可以使用
while循环
和计数器来处理任何您想要的时间。大概是

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;
long waitTime=1000;//或者你想要的超时长度
长curWaitTime=0;
while(!timeout&&curWaitTime
感谢您的快速回答。正如您在代码中看到的,我已经将time.schedule设置为100,因此我假设超时将在启动后立即发生。但事实并非如此。它发送所有文件。在我目前的位置,我无法模拟缓慢的互联网连接。。。它不在后台检查,多少时间过去了?那是什么线索。最后是睡眠(100)?它是必需的还是可以删除?顺便说一句,@publicknowledge可以让后台线程休眠100毫秒。它的必要性将取决于你在做什么。如果你需要线程睡眠来让其他事情有时间完成,那么它会起作用。要知道,这会拖那个线程一段时间,这就是为什么你通常不想在主线程上使用它的原因。例如,循环中的代码可能会访问内部设备,在再次检查之前,您需要给它时间做一些事情。我很高兴它对你有用:)