View in Android is not thread-safe, so if you update a UI control in the main thread directly in a new one, the following error is reported:
Android.view.viewrootimpl$calledfromwrongthreadexception:only the original thread that created a view hierarchy can Touc H its Views.
In order to solve the problem of updating UI controls in another thread, we can use several of the following solutions:
1. Write your own handler solution, see the use of handler in Android.
2. Using the Activity#runonuithread (Runnable) method, which receives a Runnable object as a parameter, we need to update the view in the Runnalble run method, which also uses handler inside the method.
3. Use the Postxxx method in view:
View#post (Runnable)
View#postdelayed (Runnable, long)
View#postdelayed (Runnable action, long Delaymillis)
These methods are also to receive runnable objects as parameters, but also in the Runnable run method to update the view, these methods are also implemented internally with handler.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Update UI controls in the main thread in new Android threads