【问题标题】:How to timeout Asynctask and dismiss ProgressDialog?如何使 Asynctask 超时并关闭 ProgressDialog?
【发布时间】:2013-09-27 12:12:03
【问题描述】:

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

现在我正在尝试实现超时但找不到方法,因此进度对话框会取消执行此操作。

这是我的代码:

    @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;
    }
    }
}

【问题讨论】:

    标签: android android-asynctask timeout progressdialog


    【解决方案1】:

    According to the Docs

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

    而不是做

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

    只是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;
    

    【讨论】:

    • 感谢您的快速回答。正如您在代码中看到的,我已将 time.schedule 设置为 100,因此我假设超时将在开始后立即发生。但事实并非如此。它发送所有文件。在我当前的位置,我无法模拟慢速的互联网连接......它不是在后台检查,已经过去了多少时间?
    • 最后那个 Thread.sleep(100) 是什么?它是必需的还是可以删除?顺便说一句,它满足了我的需求。
    • @publicknowledge 这将使后台线程休眠 100 毫秒。它的必要性取决于你在做什么。如果您需要线程休眠以给其他事情时间来完成,那么它将起作用。知道这会在这段时间内拖拽该线程,这就是为什么您通常不想在主线程上使用它的原因。前任。也许循环中的代码访问了一个内部设备,你需要给它时间做一些事情,然后再检查。我很高兴它对你有用:)
    猜你喜欢
    • 2015-05-12
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多