Tuesday, November 8, 2011

Cancel ProgressDialog

In the exercise "Display a indeterminate progress bar on title bar", the ProgressDialog will be dismissed by BackgroundThread after a certain time. How can user stop the waiting and force the BackgroundThread stop? we can set the ProgressDialog cancelable by calling progressDialog.setCancelable(true).

Cancel ProgressDialog

It's modified to be cancelable, such that user can cancel the waiting by BACK key. And a OnCancelListener is implemented to stop BackgroundThread. Also, a OnDismissListener is implemented. OnCancelListener will be called only when the dialog is canceled, if you needs to know when it is dismissed in general, use OnDismissListener.

package com.exercise.AndroidProgressDialog;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.Toast;

public class AndroidProgressDialogActivity extends Activity {

ProgressDialog progressDialog;
BackgroundThread backgroundThread;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);

setProgressBarIndeterminateVisibility(true);

progressDialog = ProgressDialog.show(AndroidProgressDialogActivity.this,
"ProgressDialog", "Wait!");

backgroundThread = new BackgroundThread();
backgroundThread.setRunning(true);
backgroundThread.start();

progressDialog.setCancelable(true);

progressDialog.setOnCancelListener(new OnCancelListener(){

public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
backgroundThread.setRunning(false);
Toast.makeText(AndroidProgressDialogActivity.this,
"ProgressDialog Cancelled!",
Toast.LENGTH_LONG).show();
}});

progressDialog.setOnDismissListener(new OnDismissListener(){

public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(AndroidProgressDialogActivity.this,
"ProgressDialog Dismissed!",
Toast.LENGTH_LONG).show();
}});
}



public class BackgroundThread extends Thread{
volatile boolean running = false;
int cnt;

void setRunning(boolean b){
running = b;
cnt = 10;
}

@Override
public void run() {
// TODO Auto-generated method stub
while(running){
try {
sleep(1000);
if(cnt-- == 0){
running = false;
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
handler.sendMessage(handler.obtainMessage());
}
}

Handler handler = new Handler(){

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub

setProgressBarIndeterminateVisibility(false);
progressDialog.dismiss();

boolean retry = true;
while(retry){
try {
backgroundThread.join();
retry = false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Toast.makeText(AndroidProgressDialogActivity.this,
"dismissed", Toast.LENGTH_LONG).show();
}

};
}

No comments:

Post a Comment