主源文件:
package com.example.handlerdemo;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { private Button mHandlerBtn; private Button mUpdateUIBtn; private Button mTransDataBtn; private Button mRunBtn; private Button mPostDelayButton; private EditText mEdit; private Handler mHandler; private Handler mHandlerData; private Handler mHandlerrun; private Handler mPostDelayHandler; private Message msg = new Message(); private RunThread TestThread; private int cnt = 10; class RunThread implements Runnable { @Override public void run() { // TODO Auto-generated method stub try { while (true) { //send this message every 1 seconds Thread.sleep(1000); cnt++; Bundle b = new Bundle(); b.putLong("cnt", cnt); Message mssg = new Message(); mssg.setData(b); mHandlerrun.sendMessage(mssg); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class HandlerRunMessage extends Handler { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); Bundle b = new Bundle(); b = msg.getData(); mEdit.setText("number : " +String.valueOf(b.getLong("cnt"))); } } class HandlerMessage extends Handler { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub //update ui switch (msg.what) { case 0: mEdit.setText("update ui ..."); Toast.makeText(MainActivity.this, "update ui", Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(MainActivity.this, "msg : " + 1, Toast.LENGTH_SHORT).show(); System.out.println("send message ok ..."); default: break; } super.handleMessage(msg); } } class HandleDataMessage extends Handler { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); Bundle b = msg.getData(); mEdit.setText(b.getCharSequence("content")); } } class PostDelayHandleMessage extends Handler{ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); switch(msg.what){ case 0: Toast.makeText(MainActivity.this, "post delay successfully", Toast.LENGTH_SHORT).show(); break; } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mHandler = new HandlerMessage(); mHandlerData = new HandleDataMessage(); mHandlerrun = new HandlerRunMessage(); mPostDelayHandler=new PostDelayHandleMessage(); mPostDelayButton=(Button)findViewById(R.id.button5); mPostDelayButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub final Message msg=new Message(); msg.what=0; mPostDelayHandler.postDelayed(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "under post ...", Toast.LENGTH_SHORT).show(); mPostDelayHandler.sendMessage(msg); } }, 2000); } }); mEdit = (EditText) findViewById(R.id.editText1); TestThread = new RunThread(); new Thread(TestThread).start(); mRunBtn = (Button) findViewById(R.id.button4); mRunBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub// Bundle b = new Bundle();// b.putLong("cnt", cnt);// mHandlerrun.sendMessage(msg); } }); mTransDataBtn = (Button) findViewById(R.id.button3); mTransDataBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Bundle bd = new Bundle(); // bd.putCharSequence("content", "transfer data ok !"); bd.putString("content", "transfer data OK !"); msg.setData(bd); mHandlerData.sendMessage(msg); } }); mUpdateUIBtn = (Button) findViewById(R.id.button2); mUpdateUIBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub msg.what = 0; mHandler.sendMessage(msg); } }); mHandlerBtn = (Button) findViewById(R.id.button1); mHandlerBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub msg.what = 1; mHandler.sendMessage(msg); } }).start(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}
xml文件: