1

我在一个新线程中实现了一个 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 有时会以特定百分比冻结。

4

1 回答 1

2

从上一个问题来看,您可能每次都需要创建一个新的 Message 对象。Android Handler 消息和 ListView

此外,这个讨论似乎指向 Message.clearForRecycle() 没有被调用?http://code.google.com/p/android/issues/detail?id=3318

也就是说,为什么不使用 AsyncTask 呢?它非常适合后台任务 + 发布到 UI 线程,并且与进度对话框配合得非常好。它具有用于在主后台线程工作运行之前和之后执行代码的预构建方法,并且它能够向 UI 发布进度更新,并为您很好地隐藏所有处理程序的内容。

http://androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/

http://www.android10.org/index.php/forums/43-view-layout-a-resource/908-tutorial-progressbar-running-in-asynctask

http://www.vogella.com/articles/AndroidPerformance/article.html#asynctask

于 2012-05-09T13:53:15.187 回答