先上代码 说明一下问题
- Java code
public class HandlerTextActivity extends Activity { //声明按钮变量 Button startButton,endButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取按钮实例 startButton = (Button)findViewById(R.id.startButton); endButton = (Button)findViewById(R.id.endButton); //为按钮添加事件监听器 startButton.setOnClickListener(new startButtonClickListener()); endButton.setOnClickListener(new endButtonClickListener()); } Thread thread =null; Handler handler = new Handler(); class startButtonClickListener implements OnClickListener { @Override public void onClick(View v) { // handler.post(updateThread); thread = new Thread(updateThread); thread.start(); } } class endButtonClickListener implements OnClickListener { @Override public void onClick(View v) { flag = true; } } boolean flag=false; Runnable updateThread = new Runnable() { @Override public void run() { System.out.println("updateThread"); while(true) { System.out.println("updateThread"); try { Thread.sleep(1000); if(flag) { break; } } catch (InterruptedException e) { e.printStackTrace(); } } //handler.postDelayed(updateThread, 1000); } }; }
用Handler和不用Handler都是每隔一秒输出updateThread 然后单击结束按钮就停止输出 。通过这个程序我只是觉得用Handler代码会少一点 没觉得有什么多大用处啊?用Thread都可以换他了,请大哥大姐指点一下。
------解决方案--------------------
Handler 是android提供的一种异步操作的机制。 上面的代码中的handler 其实并不是运行在其他线程中的,而是在UI主线程中,通常实现异步操作都需要通过 handler + thread的方式。
上面代码中的thread运用是完全合理的,也达到了异步执行耗时操作的要求。但并不是android推荐的方式。至于android为什么要用handler 而不是thread 可以从 android 对 handler的解释中找到一些线索。
A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.
There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.
Scheduling messages is accomplished with the post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long) methods. The post versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the sendMessage versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler's handleMessage(Message) method (requiring that you implement a subclass of Handler).
When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement timeouts, ticks, and other timing-based behavior.
When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the same post or sendMessage methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.