在Android Widget上更新UI

时间:2013-05-15 08:38:41

标签: android multithreading user-interface widget

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

以下是设置:

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);
    }

关于如何顺利实现这一目标的任何想法?

由于

0 个答案:

没有答案