Android thread processing Handler and android thread handler

Source: Internet
Author: User

Android thread processing Handler and android thread handler

The previous article has briefly introduced how to use Handler. In this article, we will discuss the advanced use of Handler. in the previous article, we just updated the UI, in this article, we will discuss how to send data from sub-threads to the main thread for processing. For this problem, I think many of my friends have been troubled by it. In fact, this is very simple, I previously felt that it was impossible to send data from the subthread to the main thread, but now I can really deny my previous point of view. It is no problem for the subthread to send data to the main thread. Well, let's discuss it together.

1. Handler and Message are used to complete:

/** Pass int type data */new Thread (new Runnable () {@ Override public void run () {try {Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace ();} Message msg = new Message (); // mes. arg1, mes. arg2, mes. what: Only int-type data msg can be uploaded. arg1 = 1; msg. arg2 = 2; msg. what = 3; handler. sendMessage (msg );}}). start ();

Process the data sent from the Message:

// Declare a Handler object and implement the handleMessage () method private Handler handler = new Handler () {public void handleMessage (android. OS. message msg) {if (msg. what = 3) {textview. setText ("msg. arg1: "+ msg. arg1 + "msg. arg2: "+ msg. arg2);} else {if (msg. what = 2) {Person person = (Person) msg. obj; textview. setText ("name:" + person. getName () + "Gender:" + person. getSex () + "Age:" + person. getYear ());}}};};

2. Message usage:

The data we send to Handler above is Int data, which certainly cannot meet our daily needs. How can we send an Object data to Handler through Message? Do not worry that the Message encapsulates an obj () method for us to transfer data.

New Thread (new Runnable () {@ Override public void run () {try {Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace ();} Person person Person = new person (); Person. setName ("Tom"); person. setSex ("girl"); person. setYear (22); Message msg = new Message (); msg. what = 2; msg. obj = person; handler. sendMessage (msg );}}). start ();}
Person is an object encapsulated by me:
public class Person {    private String name;    private String sex;    private int year;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public int getYear() {        return year;    }    public void setYear(int year) {        this.year = year;    }}

The processing of the Message data has been shown above.

If you see that Handler is powerful, Handler is very convenient when we are dealing with multithreading problems. If you are interested, you can continue to pay attention to it. The next article is more exciting.

 

 

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.