Handler学习小结,算是搞清楚了,得记下来
Handler主要用于异步消息的处理:当发送一个消息之后,首先进入一个消息队列,发送消息的函数即可返回,而另外一个部分逐个的在消息队列中将消息取出,然后对消息进行出来,就是发送消息和接收消息不是同步的处理。这种机制通常用来处理相对好是比较长的操作。(就如post方法是讲Runnable对象添加到主线程中,通俗的说:这里的代码中主要用Handler通知主线程,在主线程中处理异步结果,当线程中耗时任务完成后,例如数据准备,就通知主线程更新(如:更新UI),Handler的hand)
Handler 常用方法:
post(Runnable,long)
postDelayed(Runnable,long);
sendEmptyMessage(int What);
sendMessage(Message);
sendMessageAtTime(Message,long);
sendMessageDelayed(Message,long);
以上post方法允许你安排一个Runnable对象到主线程队列 中,
sendMessage 方法,允许你安排一个带数据的Message对象到队列中,等待更新
- package com.handler.test;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class Handler1Activity extends Activity {
- private Button button = null;
- private TextView textview = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button) findViewById(R.id.button);
- textview = (TextView) findViewById(R.id.textview);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- new Thread(thread).start();
- try {
- Thread.sleep(2000);
- Log.i("Handler1Activity", "主线程");
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- }
- private Handler handler = new Handler(){
- @Override
- public void handleMessage(Message msg) {
- if(msg.what==1){
- textview.setText("处理完成");
- System.out.println("handler"+Thread.currentThread().getId());
- }
- }
- };
- private Thread thread = new Thread(){
- @Override
- public void run() {
- //这里处理异步消息
- Log.i("Handler1Activity", Thread.currentThread().getId()+"异步处理");
- handler.sendEmptyMessage(1);
- }
- };
- }
package com.handler.test;import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView;public class Handler1Activity extends Activity {private Button button = null;private TextView textview = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);button = (Button) findViewById(R.id.button);textview = (TextView) findViewById(R.id.textview);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {new Thread(thread).start();try {Thread.sleep(2000);Log.i("Handler1Activity", "主线程");} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});}private Handler handler = new Handler(){@Overridepublic void handleMessage(Message msg) {if(msg.what==1){textview.setText("处理完成");System.out.println("handler"+Thread.currentThread().getId());}}};private Thread thread = new Thread(){@Overridepublic void run() {//这里处理异步消息Log.i("Handler1Activity", Thread.currentThread().getId()+"异步处理");handler.sendEmptyMessage(1);}}; }
这里的
- System.out.println("handler"+Thread.currentThread().getId());//handler1