Android Development Thread

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.