Wie timeout Asynctask und entlassen ProgressDialog?

Ich habe da ein problem und hoffe Ihr könnt mir helfen.
Ich habe einen Asynctask, startet das hochladen von Daten, wenn ich eine Taste drücken in meine mainactivity.
Es funktioniert gut, außer wenn ich habe eine langsame internet-Verbindung.
Der Asynctask gestartet wird ein progressdialog und wenn ich habe eine langsame Verbindung, Asynctask beendet, aber der Progressdialog nicht verschwinden, weil es nie erreicht onPostExecute.

Nun bin ich versucht zu implementieren ein timeout, aber kann nicht einen Weg finden, so dass der progressdialog entlässt, dies zu tun.

Hier ist mein 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;
    }
    }
}
InformationsquelleAutor Har Un | 2013-09-22
Schreibe einen Kommentar