How do we use runOnUiThread in Android?

前端 未结 12 771
太阳男子
太阳男子 2020-11-22 00:09

I\'m new to Android and I\'m trying to use the UI-Thread, so I\'ve written a simple test activity. But I think I\'ve misunderstood something, because on clicking the button

相关标签:
12条回答
  • 2020-11-22 00:40

    Below is corrected Snippet of runThread Function.

    private void runThread() {
    
        new Thread() {
            public void run() {
                while (i++ < 1000) {
                    try {
                        runOnUiThread(new Runnable() {
    
                            @Override
                            public void run() {
                                btn.setText("#" + i);
                            }
                        });
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
    
    0 讨论(0)
  • 2020-11-22 00:40

    This is how I use it:

    runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                    //Do something on UiThread
                }
            });
    
    0 讨论(0)
  • 2020-11-22 00:41

    We use Worker Thread to make Apps smoother and avoid ANR's. We may need to update UI after the heavy process in worker Tread. The UI can only be updated from UI Thread. In such cases, we use Handler or runOnUiThread both have a Runnable run method that executes in UI Thread. The onClick method runs in UI thread so don't need to use runOnUiThread here.

    Using Kotlin

    While in Activity,

    this.runOnUiThread {
          // Do stuff
    }
    

    From Fragment,

    activity?.runOnUiThread {
          // Do stuff
    }
    

    Using Java,

    this.runOnUiThread(new Runnable() {
         void run() {
             // Do stuff
         }
    });
    
    0 讨论(0)
  • 2020-11-22 00:42
    runOnUiThread(new Runnable() {
                    public void run() {
                    //Do something on UiThread
                }
            });
    
    0 讨论(0)
  • 2020-11-22 00:42

    thy this:

    @UiThread
        public void logMsg(final String msg) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Log.d("UI thread", "I am the UI thread");
    
    
                }
            });
        }
    
    0 讨论(0)
  • 2020-11-22 00:46

    You have it back-to-front. Your button click results in a call to runOnUiThread(), but this isn't needed, since the click handler is already running on the UI thread. Then, your code in runOnUiThread() is launching a new background thread, where you try to do UI operations, which then fail.

    Instead, just launch the background thread directly from your click handler. Then, wrap the calls to btn.setText() inside a call to runOnUiThread().

    0 讨论(0)
提交回复
热议问题