我在一个新线程中实现了一个 ProgressDialog。
onCreate 中的代码
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Verifying...");
progressDialog.setCancelable(true);
progressDialog.show();
t.start();
线程声明
Runnable r = new MyRunnable();
Thread t = new Thread(r);
可运行
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 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);
}
}
};
现在我面临两个问题
1、ProgressDialog被dismiss时Logcat出现如下错误信息
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 有时会以特定百分比冻结。