Android's network programming

Source: Internet
Author: User

Attention:

(1) The main thread is not able to do time-consuming operations, network requests are time-consuming operations need to be put into sub-threading.
(2) The child thread cannot update the contents of the control (update UI). So there is a contradiction, the solution is to use handler (processing message mechanism).

Handler: How message mechanisms are written (important)

Steps to use handler:

1. Create a handler in the main thread

Private New Handler () {    publicvoid  handlemessage (android.os.Message msg) {    };} ;

2. Handlermessage method for overriding handler

3. Create a Message object in the child thread to bind the obtained data to the MSG

New Message (); // Another way: Message msg = Messge.obtain; Msg.obj = result;

4. The handler object in the main thread sends a message to the main thread in a child thread

Handler.sendmessage (msg);

5. The Handlermessage method in the main thread accepts data from a child thread and can do an update of the UI.

Android's network programming

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.