1、onCreate()中写好handler的接受机制,准备接受并处理消息
2、thread中利用handler.post(Runnable r):
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.text); handler = new Handler(); // new myHandlerThread(handler).start(); new myHandlerThread2(handler).start(); } class myHandlerThread2 extends Thread { private Handler handler; public myHandlerThread2(Handler handler) { // TODO Auto-generated constructor stub this.handler = handler; } @Override public void run() { // TODO Auto-generated method stub super.run(); handler.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub textView.setText("myHandlerThread2"); } }); } }
时间: 2024-11-05 11:46:52