. 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 ;}}
??