Reputation: 19102
I have implemented a ProgressDialog in a new thread.
Code in onCreate
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Verifying...");
progressDialog.setCancelable(true);
progressDialog.show();
t.start();
Thread declaration
Runnable r = new MyRunnable();
Thread t = new Thread(r);
Runnable
public class MyRunnable implements Runnable {
@Override
public void run() {
Message msg = Message.obtain();
if (t.isInterrupted()) {
msg.recycle();
return;
}
long startTime = SystemClock.elapsedRealtime();
long duration = 5 * 60 * 1000; // 5 min
long percentageIntervalDuration = 2;
long interval = (duration * percentageIntervalDuration) / 100; // 6sec
int i = 0;
do {
if (SystemClock.elapsedRealtime() == (startTime + (i * interval))) {
msg.arg1 = (int) percentageIntervalDuration * i;
mainHandler.sendMessage(msg);
i++;
}
} while (i != (100 / percentageIntervalDuration) + 1);
}
}
Handler attached to Main Thread
Handler mainHandler = new Handler() {
public void handleMessage(Message msg) {
Log.d(TAG, String.valueOf(msg.arg1));
if (msg.arg1 == 100) {
progressDialog.dismiss();
} else {
progressDialog.setProgress(msg.arg1);
}
}
};
Now I am facing two problems
1. The following error messages appear in Logcat when ProgressDialog is dismissed
05-09 17:57:36.386: E/JavaBinder(9822): *** Uncaught remote exception! (Exceptions are not yet supported across processes.) 05-09 17:57:36.386: E/JavaBinder(9822): android.util.AndroidRuntimeException: { what=4 when=650507787 arg1=1 } This message is already in use. 05-09 17:57:36.386: E/JavaBinder(9822): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:171) 05-09 17:57:36.386: E/JavaBinder(9822): at android.os.Handler.sendMessageAtTime(Handler.java:457) 05-09 17:57:36.386: E/JavaBinder(9822): at android.os.Handler.sendMessageDelayed(Handler.java:430) 05-09 17:57:36.386: E/JavaBinder(9822): at android.os.Handler.sendMessage(Handler.java:367) 05-09 17:57:36.386: E/JavaBinder(9822): at android.view.inputmethod.InputMethodManager$1.setActive(InputMethodManager.java:428) 05-09 17:57:36.386: E/JavaBinder(9822): at com.android.internal.view.IInputMethodClient$Stub.onTransact(IInputMethodClient.java:83) 05-09 17:57:36.386: E/JavaBinder(9822): at android.os.Binder.execTransact(Binder.java:288) 05-09 17:57:36.386: E/JavaBinder(9822): at dalvik.system.NativeStart.run(Native Method) 05-09 17:57:36.386: E/JavaBinder(9822): *** Uncaught remote exception! (Exceptions are not yet supported across processes.) 05-09 17:57:36.386: E/JavaBinder(9822): android.util.AndroidRuntimeException: { what=100 when=650507787 } This message is already in use. 05-09 17:57:36.386: E/JavaBinder(9822): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:171) 05-09 17:57:36.386: E/JavaBinder(9822): at android.os.Handler.sendMessageAtTime(Handler.java:457) 05-09 17:57:36.386: E/JavaBinder(9822): at android.os.Handler.sendMessageDelayed(Handler.java:430) 05-09 17:57:36.386: E/JavaBinder(9822): at android.os.Handler.sendMessage(Handler.java:367) 05-09 17:57:36.386: E/JavaBinder(9822): at com.android.internal.view.IInputConnectionWrapper.dispatchMessage(IInputConnectionWrapper.java:182) 05-09 17:57:36.386: E/JavaBinder(9822): at com.android.internal.view.IInputConnectionWrapper.reportFullscreenMode(IInputConnectionWrapper.java:159) 05-09 17:57:36.386: E/JavaBinder(9822): at com.android.internal.view.IInputContext$Stub.onTransact(IInputContext.java:211) 05-09 17:57:36.386: E/JavaBinder(9822): at android.os.Binder.execTransact(Binder.java:288) 05-09 17:57:36.386: E/JavaBinder(9822): at dalvik.system.NativeStart.run(Native Method)
2. ProgressDialog freezes sometimes at a particular percentage.
Upvotes: 1
Views: 2368
Reputation: 2101
From this previous question, it looks like you may need to create a new Message object every time. Android Handler Message and ListView
Also, this discussion seems to point to Message.clearForRecycle() not being called? http://code.google.com/p/android/issues/detail?id=3318
That said, why not use an AsyncTask? It's pretty much geared for Background Tasks + Publishing to the UI thread and works really well with Progress Dialogs. It has pre-built methods for executing code before and after the main background threaded work is run and it has the capability to publish progress updates to the UI with all the handler stuff nicely hidden for you.
http://androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/
http://www.vogella.com/articles/AndroidPerformance/article.html#asynctask
Upvotes: 2