Android implements the method of updating the UI in the activity in the child thread

This article gives an example of how Android implements the method of updating the UI in the activity in the child thread. Share with you for your reference, as follows:

In the Android platform, when multithreading programming, it is often necessary to perform some processing in a separate thread other than the main thread, and then update the user interface display. However, the problem of directly updating the page display in threads other than the mainline thread is that the system will report this exception:

ERROR/AndroidRuntime(1222): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Perhaps the programmer will call the Toast.makeText () method in the thread and try to display some hints in UI.

Can't create handler inside thread that has not called Looper.prepare()

Solution: the sub thread cannot directly update the UI in the activity. The general method is that the sub thread sends messages to the activity, and then the activity updates the UI according to these messages. Android has a class called android.os.handler, which is used to do this.

1. Declare a variable of android.os.handler class in the activity whose UI needs to be updated by the thread,

2. Add handler initialization in oncreate function:

In addition, the get function of handler needs to be provided in the activity, so that the thread can get the handler and then pass the message.

3. The sub thread class needs to hold the context class object representing the context. In practical application, this reference refers to the activity object to update the UI, which is generally declared as:

Then initialize CTX in the sub thread class constructor or other functions. This step is to get the handler object in the activity object. (or use other methods, as long as the child thread can get the handler object in the activity.)

4. In the last step, when the sub thread runs to a certain place and needs to deliver messages to the activity, create an object of android.os.message class, add the object to be delivered to the message, and send it to the main thread through the handler. The code example is as follows:

Remember, the handler here is the same object as the handler in the activity. Oh, this is the way to send messages to that activity.

In addition, this method can not only let the child thread update the UI, but also have other purposes. Now let's assume that the child thread may throw some errors, which should be normal. How to let the user know the error information? Very simply, in the catch statement block, put the catch error object into message.obj and pass it to the activity. In the activity, use the toast. Maketext() method to display the error message.

For more Android related content, readers who are interested can view the special topics of this site: summary of Android thread and message mechanism usage, summary of activity operation skills of Android programming, summary of Android debugging skills and common problem solutions, introduction and advanced tutorial of Android development, summary of Android multimedia operation skills (audio, video, recording, etc.) Android basic component usage summary, Android view skills summary, Android layout skills summary and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>