How to timeout Asynctask and dismiss ProgressDialo

2019-04-11 12:47发布

问题:

I've got a problem and hope you can help me. I've got an Asynctask, which starts uploading data if I press a button in my mainactivity. It works fine except if I've got a slow internet connection. The Asynctask starts a progressdialog and if I've got a slow connection, Asynctask stops but the Progressdialog doesn't disappear because it never reached onPostExecute.

Now I'm trying to implement a timeout but can't find a way, so that the progressdialog dismisses to do this.

Here is my code:

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

回答1:

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;