消息机制:
handler 主线程和其他线程的桥梁,消息队列。
handler 的handMessage()方法由主线程控制,UI的交互也在这个方法里操作;setMssage()方法由自己创建的线程调用来发送消息。他们之间通过变量Message来标识(what类成员变量)。
实例代码:
package com.android.listview; import com.android.port.Action; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MenuActivity extends Activity { protected static final int MSG = 0; String[] optionMenu = { "新建", "保存", "更新" }; private Handler handler = new Handler(){ public void handleMessage(Message msg) { if(msg.what == MSG){ Toast.makeText(MenuActivity.this, "更新完成", 1000).show(); } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO 自动生成的方法存根 for (int i = 0; i < optionMenu.length; i++) { menu.add(0, i, i, optionMenu[i]); } return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int itemId = item.getItemId(); if (itemId == 0) { // 提示 new NewAction().execute(); } else if (itemId == 1) { // 单选 new SaveAction().execute(); } else if (itemId == 2) { //更新 new UpdateAction().execute(); } return super.onOptionsItemSelected(item); } class NewAction implements Action { @Override public void execute() { // TODO 自动生成的方法存根 // 提示 AlertDialog.Builder builder = new AlertDialog.Builder( MenuActivity.this); builder.setTitle("确定"); builder.setMessage("确定要新建吗?"); builder.setNegativeButton("确定", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(MenuActivity.this, "已经新建", 500).show(); } }); builder.setPositiveButton("取消", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(MenuActivity.this, "取消新建", 500).show(); } }); AlertDialog dialog = builder.create(); dialog.show(); } } class SaveAction implements Action { int position = 0; String[] num = new String[] { "One", "Two", "Three" }; @Override public void execute() { // TODO 自动生成的方法存根 //保存 AlertDialog.Builder builder = new AlertDialog.Builder( MenuActivity.this); builder.setTitle("单选"); builder.setSingleChoiceItems(num, 0, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MenuActivity.this, num[which], 500).show(); position = which; } }); builder.setNegativeButton("确定", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(MenuActivity.this, num[position] + "已经保存", 500).show(); } }); builder.setPositiveButton("取消", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(MenuActivity.this, num[position] + "取消保存", 500).show(); } }); AlertDialog dialog = builder.create(); dialog.show(); } } class UpdateAction implements Action { ProgressDialog pd; Message msg = new Message(); @Override public void execute() { // TODO 自动生成的方法存根 AlertDialog.Builder builder = new AlertDialog.Builder(MenuActivity.this); builder.setTitle("更新"); builder.setItems(new String[] { "One", "Two", "Three" }, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO 自动生成的方法存根 pd = new ProgressDialog(MenuActivity.this); pd.setTitle("稍等片刻"); pd.setMessage("一会就好,相信我没死机~"); pd.show(); new Thread() { @Override public void run() { super.run(); try { Thread.sleep(10000); pd.dismiss(); msg.what=MSG; handler.sendMessage(msg); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); } }); AlertDialog dialog = builder.create(); dialog.show(); } } }
谢谢~