3

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;
    }
    }
}
Har Un
  • 33
  • 1
  • 5

1 Answers1

6

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;
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • thank you for your quick answer. As you can see in the code, I've set the time.schedule to 100, therefore i assume that the timeout will occur right after the start. but it doesn't. it sends all the files. In my current location, I can't simulate a slow internet connection... Doesn't it check in the background, how much time has passed? – Har Un Sep 22 '13 at 15:00
  • what is that Thread.sleep(100) at the end? Is it required or can it be removed? it worked for my needs btw. – publicknowledge Dec 10 '15 at 04:45
  • 1
    @publicknowledge That will put the background thread to sleep for 100ms. It's necessity will depend on what you are doing. If you need the thread to sleep to give other things time to finish then it will work for that. Know that this will hault that thread for that amount of time which is why you don't want to use it on the main thread usually. Ex. maybe the code in the loop access an internal device and you need to give it time to do something before checking again. I'm glad it worked for you :) – codeMagic Dec 10 '15 at 20:59