. Net programmers play Android Development --- (17) Handler usage

Source: Internet
Author: User

. Net programmers play Android Development --- (17) Handler usage

In android development, if you want to update the data displayed by the control on the main interface in a thread, assigning a value to the control on the main interface causes an exception. For security reasons, it is not allowed to update the interface control data in the thread. In this case, we can use Handler. handler is a component that processes message transmission and communication between interfaces and threads. The following describes two methods for handler to handle messages in detail. The following column shows the methods for passing messages by clicking different buttons: handler. post and handler. sendmsg.

1. Handler. Post

Create a handler object and Runnable object, and update the textview data in the interface through handler. post (new Runnable () {});

2. Handler. sendMessage

In this example, we create an employee class and click the button to display the employee information. The handler. sendmessage is used to transmit messages and parameters for processing.

 

 

Package com. example. helloword; import android. r. string; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. simpleAdapter; import android. widget. textView; public class HandlerActivity extends Activity {private TextView TV; private Button btnpost; private Button btnmsg; private Handler handler = new Handler (); // post handler // sendmsg handlerprivate Handler handlerTwo = new Handler () {public void handleMessage (Message msg) {switch (msg. what) {case 0: TV. setText (Name: + (employee) msg. obj ). name); break ;}}; protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. handlerlayout); TV = (TextView) findViewById (R. id. textViewMsg); btnpost = (Button) findViewById (R. id. btnpost); btnmsg = (Button) findViewById (R. id. btnmsg); btnpost. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubThread thread = new Thread (new Runnable () {@ Overridepublic void run () {// TODO Auto-generated method stubhandler. post (runOne) ;}}); thread. start () ;}}); btnmsg. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubThread thread = new Thread (new Runnable () {@ Overridepublic void run () {// TODO Auto-generated method stubMessage msg = new Message (); msg. arg1 = 1; employee employeeone = new employee (); employeeone. name = EMPLOYEE 1; employeeone. age = 20; msg. obj = employeeone; msg. what = 0; handlerTwo. sendMessage (msg) ;}}); thread. start () ;}}) ;}runnable runOne = new Runnable () {@ Overridepublic void run () {// TODO Auto-generated method stubtv. setText (handler post is being executed) ;}}; public class employee {public String name; public int age ;}}

 

 

 

??

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.