android runonuithread
时间: 2024-05-14 08:15:12 浏览: 82
Android `runOnUiThread` is a method that allows you to execute code on the UI thread. The UI thread is also known as the main thread, and it is responsible for handling user interface events such as drawing the screen, responding to user input, and updating the view hierarchy.
When you call `runOnUiThread`, the code you provide is queued up to be executed on the UI thread. This is useful when you need to update the UI from a background thread or perform some other action that must be executed on the UI thread.
Here is an example of using `runOnUiThread` to update a TextView with some text:
```
runOnUiThread(new Runnable() {
public void run() {
TextView textView = findViewById(R.id.text_view);
textView.setText("Hello, world!");
}
});
```
In this example, we create a new `Runnable` object that contains the code we want to execute on the UI thread. We then call `runOnUiThread` and pass in this `Runnable` object.
Inside the `run` method of the `Runnable` object, we obtain a reference to a TextView and set its text to "Hello, world!". Because we are running this code on the UI thread, the text will be updated immediately and the user will see the new text on the screen.
阅读全文