Threads (android)
in java we learn thread, thread, is a unit of the process, when the program to run, will open the thread, run the program, we want to create a thread we need to inherit the interface thread or implement the Runable interface, and then rewrite the Run method, You call the start () method of the thread class to turn on the thread. The same is true for Android. However, there is one more limit on Android: only the main thread can modify the interface . When the application starts,Android first opens a main thread (ie UI thread), UI controls in the main thread management interface, and event distribution, and the thread we created is not able to modify the interface directly. So we need to use the handler class to help us modify the interface ;
Handler class:
mainly accepts the data sent by the child thread, and updates with this data with the main thread Ui.
Write a handler class, inherit handler, rewrite the Handlermessage () method, and then modify the interface inside the method.
Use the object of the message class to pass the message, and then receive the information in Handlermessage () to modify the interface's operation.
Practical Walkthrough:
In the interface to achieve the moving ball, every click on the interface, there will be a small ball;
1. Implement a custom component to add to the form; In a custom component, you can add a small ball to a small ball object in an array.
2. implement a thread class, take out the objects in our array, draw them in the thread's Run method, and instantiate a message object to send our bitmap bitmap;
3. Implement our Handler class, rewrite the Handlermessage () method, and receive messages from our message.
After the interface receives our message, it is possible to put a bitmap on the interface.
Such a simple ground-path game is realized;
Android Development Thread