示例:下面代码的功能是修改UI主线程TextView的内容
public class MainActivity extends Activity { private Button btn_start; private TextView numTxtView; int i = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); numTxtView = (TextView) this.findViewById(R.id.numTxtView); btn_start = (Button)this.findViewById(R.id.btn_start); final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0x1233) { //修改主线程中TextView所显示的内容 numTxtView.setText("" + i); i++; } } }; btn_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //定义一个计时器,让改计时器周期性地执行指定任务 new Timer().schedule(new TimerTask() { @Override public void run() { //发送空消息 handler.sendEmptyMessage(0x1233); } }, 0,1000); } }); } }