Saturday, October 17, 2009

[android-developers] Re: Activity managed indeterminate ProgressDialog problem

Small things like this drive me crazy. If anyone could hint what I am
doing wrong in the code below (full example of the issue described
previously in this thread), I would be very grateful.

Thank you,

-szabolcs

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AsyncTaskTest extends Activity {
static final int DLG_PROGRESS = 0;
static String strDlgProgressMessage = "";

DaTask mTask = null;
Button mBtnRunTask = null;

// SET THE BOOLEAN BELOW TO CHANGE BEHAVIOR
static final boolean USE_DLG_REMOVE_INSTEAD_OF_DISMISS = false;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mBtnRunTask = new Button(AsyncTaskTest.this);
mBtnRunTask.setText("Run Task");
mBtnRunTask.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View btn) {
btn.setEnabled(false);
mTask = new DaTask();
mTask.execute(new Integer(121));
}
});

setContentView(mBtnRunTask);
}

@Override
public Dialog onCreateDialog(int idDlg) {
Dialog dlg = null;

switch (idDlg) {
case DLG_PROGRESS:
ProgressDialog pDlg = new ProgressDialog(AsyncTaskTest.this);
pDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDlg.setCancelable(true);
pDlg.setOnCancelListener(
new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (mTask != null && mTask.getStatus() !=
AsyncTask.Status.FINISHED) {
mTask.cancel(true);
}
}
}
);
dlg = pDlg;
break;
}

return dlg;
}

@Override
public void onPrepareDialog(int idDlg, Dialog dlg) {
switch (idDlg) {
case DLG_PROGRESS:
((ProgressDialog)dlg).setMessage(strDlgProgressMessage);
break;
}
}

public class DaTask extends AsyncTask<Integer, Void, Double> {
@Override
protected Double doInBackground(Integer... aNum) {
Double result = 0.0;
for (int i=0; i<100000; i++) {
if (isCancelled())
break;
result = Math.sqrt(aNum[0]);
}
return result;
}

@Override
public void onPreExecute() {
AsyncTaskTest.strDlgProgressMessage = "Running task,\nplease
wait..";
showDialog(AsyncTaskTest.DLG_PROGRESS);
}

@Override
public void onPostExecute(Double result) {
if (USE_DLG_REMOVE_INSTEAD_OF_DISMISS)
removeDialog(AsyncTaskTest.DLG_PROGRESS);
else
dismissDialog(AsyncTaskTest.DLG_PROGRESS);
mBtnRunTask.setEnabled(true);
}

@Override
public void onCancelled() {
if (USE_DLG_REMOVE_INSTEAD_OF_DISMISS)
removeDialog(AsyncTaskTest.DLG_PROGRESS);
else
dismissDialog(AsyncTaskTest.DLG_PROGRESS);
mBtnRunTask.setEnabled(true);
}
}
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments:

Post a Comment