簡體   English   中英

后台線程中的 Progress bar.setProgress(variable)

[英]Progress bar.setProgress(variable) in a background thread

這是我關於堆棧的第一個問題(很棒的社區,謝謝!)

所以,我有這個問題:

我正在開發一個帶有選項卡布局的 android 應用程序。 每當我導航到“狀態”選項卡時,我都想顯示一個根據變量值更新的圓形ProgressBar 讓我們稱這個變量為prog 此外,我想在 ProgressBar 的中心顯示一個TextView ,它顯示與prog相同的值。

我想出了這部分代碼:

public class StatusFragment extends Fragment
{

    private ProgressBar torqueRefProgressBar;
    private TextView torqueRefTextView;


    RunningThreadExample r;
    private Thread t1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        //Inflate the layout for this fragment
        return ...;

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        // Initializations and other parts not relevant for the question...

        this.torqueRefProgressBar   =   getView().findViewById(R.id.torqueRefProgressBar);

        this.torqueRefTextView      =   getView().findViewById(R.id.torqueRefTextView);


        t1 = new Thread( this.torqueRefProgressBar, this.torqueRefTextView)); // passing arguments by value but values are references to the objects, therefore I'm passing by ref

        t1.start();


    }

}

在這個片段中,我創建了 ProgressBar 和 TextView,然后將它們傳遞給 Runnable,如下所示

public class RunningThreadExample implements Runnable
{

    ProgressBar torqueRefProgBar_thread;
    TextView torqueRefTextView_thread;

    public RunningThreadExample(ProgressBar progressBar, TextView textView)
    {
        this.torqueRefProgBar_thread = progressBar;
        this.torqueRefTextView_thread = textView;
        this.endScale = this.torqueRefProgBar_thread.getMax();
    }

    @Override
    public void run()
    {
        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

       float i = 0;

       double temp = 0;


        while(Running)
        {
            try
            {
                Thread.sleep(1000/60); // update freq 60Hz
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }

            temp = getCurrentVariableValue(); // Not shown here (returns a double)

            this.torqueRefProgBar_thread.setProgress((int) temp);
            this.torqueRefTextView_thread.setText(String.valueOf(temp));

        }

        Log.i(INFO_TAG,"Finishing thread!!!!!");

    }


}


我希望此更新在應用程序運行時一直運行(因此我忽略了AsyncTask )。 一段時間后,ProgressBar 會停止更新,而 TextView 繼續正常工作,沒有任何問題。

我一直在讀到一個可能的原因可能是調用setProgress(temp)可能會卡住 UI。 但是,當 TextView 仍在工作時,我無法理解。

你能建議一個更好的方法來更新進度條嗎?

謝謝!

看到這個線程:

Android 基礎:在 UI 線程中運行代碼

總而言之,Android 需要某些類型和部分的代碼才能在特定上下文中運行。 許多工具(例如處理程序)可用於幫助完成這些任務,但開始時很難學習。

您不能從主線程以外的任何線程影響視圖。 run()方法中run()以下操作

runOnUiThread(new Runnable() { 
// 
run(){

 this.torqueRefProgBar_thread.setProgress((int) temp);
      }      this.torqueRefTextView_thread.setText(String.valueOf(temp));
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM