1

我有一个带有按钮和进度条的小部件。基本上,当按下按钮时,进度条开始填满。这相对简单 - 但随着进度条填满,它似乎滞后了。我让它在一个单独的线程中运行,但仍然 - 整个系统似乎很紧张。

这是设置:

pdateProgressBar = new Runnable() {

        @Override
        public void run() {
            if (panicStatus) {
                // Start the progress bar
                // At the end ( < 100)
                progress += 0.33f;
                remoteViews.setProgressBar(R.id.widgetProgress, 100, (int)progress, false);

                // Update UI
                thisWidget = new ComponentName(main, WidgetProvider.class);
                AppWidgetManager manager = AppWidgetManager.getInstance(main);
                manager.updateAppWidget(thisWidget, remoteViews);

                if (progress < 100) {
                    mHandler.postDelayed(this, 10);
                } else {
                    mHandler.removeCallbacks(updateProgressBar);
                    mHandler = null;
                }
            } else {
                mHandler.removeCallbacks(this);
                mHandler = null;
            }
        }

    };

    Runnable update = new Runnable() {

        @Override
        public void run() {
            if (panicStatus) {
                Looper.prepare();
                Log.d("WIDGET", "Creating new Handler...");
                mHandler = new Handler(); 
                Log.d("WIDGET", "Starting runnable updateProgressBar");
                mHandler.postDelayed(updateProgressBar, 10);
                Looper.loop();

            }
        }
    };


    if (!panicStatus) {
        // Panic pressed, allow time to cancel
        Log.d("WIDGET", "PANIC");
        remoteViews.setTextViewText(R.id.widget_panic_button, "CANCEL");
        panicStatus = true;
        progress = 0;   

        // Start new handler
        // Update progress bar

        new Thread(update).start();


    } else {
        // Cancel pressed, cancel pending Panic
        Log.d("WIDGET", "CANCEL");
        remoteViews.setTextViewText(R.id.widget_panic_button, "PANIC");
        panicStatus = false;
        progress = 0;
        remoteViews.setProgressBar(R.id.widgetProgress, 100, 0, false);
    }

关于如何让这一切顺利进行的任何想法?

谢谢

4

0 回答 0