首页 > 代码库 > Handler

Handler

Handler主要用于接受子线程发送的数据, 并用此数据配合主线程更新UI,因为更新UI只能在主线程中进行。

Handler作为线程通讯工具类。用于传递消息。它有两个队列:

  • 消息队列使用sendMessage和HandleMessage的组合来发送和处理消息。
  • 线程队列类似一段代码,或者说一个方法的委托,用户传递方法。使用post,postDelayed 添加委托,使用 removeCallbacks移除委托。

下例给出两个个简单用法:

  • 采用Thread:
public class MainActivity extends Activity {    private TextView mTextView;        private Handler handler = new Handler() {                 @Override        public void handleMessage(Message msg) {            // 接收消息并且去更新UI线程上的控件内容            if (msg.what == 0) {                mTextView.setText(String.valueOf(msg.obj));            }            super.handleMessage(msg);        }    };        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                mTextView = (TextView) findViewById(R.id.text);                new Thread() {            @Override            public void run() {                try {                    for (int i = 0; i < 100; i++) {                        Thread.sleep(500);                        Message msg = new Message();                        msg.what = 0;                        msg.obj = "更新后的值:" + i;                        handler.sendMessage(msg);//向主线程的handler发消息                    }                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }.start();    }}
  •  采用runable 和 postDealy:
public class MainActivity extends Activity {    private static final String TAG_ACTIVITY = "MainActivity";    private static int tick = 0;    private TextView mTextView;    private Button mbutton;        private Handler handler = new Handler() {                 @Override        public void handleMessage(Message msg) {            // 接收消息并且去更新UI线程上的控件内容            super.handleMessage(msg);            if (msg.arg1 == 0) {                mTextView.setText(String.valueOf(msg.obj));            }                        //重新把线程加入线程队列,延迟1000ms            handler.postDelayed(updateThread,1000);        }    };        Runnable updateThread = new Runnable(){                @Override        public void run() {                          Message msg = handler.obtainMessage();            msg.arg1 = 0;            msg.obj = "更新后的值" + tick++;                                  if(10 == tick){                //移除该线程                handler.removeCallbacks(updateThread);                return;            }                        handler.sendMessage(msg);//向主线程的handler发消息            Log.i(TAG_ACTIVITY, String.valueOf(msg.obj));         }    };        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                mTextView = (TextView) findViewById(R.id.text);        mbutton   = (Button) findViewById(R.id.button1);                mbutton.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View v) {                 //把线程加入线程队列                 handler.post(updateThread);                            }        });    }}

 

Handler