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