public class

Handler

extends Object

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.

Handler的基本概念

包含线程队列和消息队列,实现异步的消息处理机制,跟web开发的ajax有异曲同工之妙。

创建一个Handler对象

Handler handler = new Handler();

将要执行的操作写在线程对象的run方法当中

Runnable updateThread = new Runnable(){    public void run(){        handler.postDelayed(updateThread,3000);}}

一个点击监听事件,在handler中添加要执行的线程

Class StartButtonListener implements OnClickListener{    public voidonClick(View v){        //调用Handler的post方法,将要执行的线程对象添加到队列中        handler.post(updateThread);        }}

一个点击监听事件,在handler中把线程移除

Class EndButtonListerger implements OnClickListener(){    public void onClick(View v){        handler.removeCallBacks(updateThread);        }}

使用Handler更新ProgressBar的方法

分别使用

updateBarHandler.post(updateThread);

updateBarHandler.sendMessage(msg);

传递线程和消息。

通过Handler的handleMessage()方法处理消息。

首先使用匿名内部类来复写Handler当中的handlerMessage方法,该方法用于接收消息

Handler updateBarHandler = new Handler(){    public void handleMessage(Message msg){        bar.setProgress(msg.arg1);        updateBarHandler.post(updateThread);        }};

创建一个线程类,用于控制进度条的进度,该类使用匿名内部类的方式进行声明

Runnable updateThread = new Runnable(){    int i = 0;    public void run(){        i = i + 10;        Message msg = updateBarHandle.obtainMessage();        msg.arg1 = i;        try{            Thread.sleep(1000);                } catch(InterruptedException e) {                    e.printStackTrack();                }                //将msg对象加入到消息队列                updateBarHandler.sendMessage(msg);                if( i == 100){                    updateBarHandler.removeCallbacks(updateThread);                }        }}

点击按钮事件,加入队列,启动线程

class ButtonListener implements OnClickListener{    public void onClick(View v){        bar.setVisibility(View.VISIBLE);        //线程队列        updateBarHandler.post(updateThread);}}

Handler中使用单独的线程

需要使用public Handler (Looper looper)

构造函数创建Handler对象。从而实现使用Looper来处理消息队列的功能。借助Bundle使用Message传递数据。

当使用

handler.post(new Runnable);

时Handler和Activity处于同一个线程当中,在同一个线程中调用run方法。

而使用

Thread t = new Thread(new Runnable());

t.start();

创建的线程和Activity处于不同的线程中

Bundle的用法

可以把Bundle看成是特殊的map,所有的key都是String类型的

在新线程当中处理消息的方法

protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    //打印当前线程的ID    System.out.println(Thread.currentThread().getId());    //生成一个HandlerThread对象,实现了Looper来处理消息队列的功能,这个类由android应用框架提供    HandlerThread handlerThread = new HandlerThread("hanlderThread");    //必须先调用该类的start的方法,否则该获取的Looper对象为NULL    handlerThread.start();    MyHanlder myHandler = new MyHandler(handlerThread.getLooper());    Message msg = myHandler.obtainMessage();    Bundle bundle = new Bundle();    bundle.putString("name","arthinking");        msg.setData(b);    msg.sendToTarget();}class MyHandler extends Handler{    public MyHandler(){}    public MyHandler(Looper looper){        super(looper);        }        //每当有消息进入消息队列时就执行这个函数对消息进行操作        public void handleMessage(Message msg){            Bundle bundle = msg.getData();            String name = bundle.getString("name");            System.out.println(Thread.currentThread().getId());            System.out.println(name);        }}

更多相关文章

  1. Android设计一个答题APP
  2. Android(安卓)推送 之 JPush
  3. Android中Handler机制
  4. Android推送通知指南
  5. android开启线程的误区
  6. Android推送通知指南
  7. Android之进程与线程
  8. Android之消息推送实现
  9. Android(安卓)内存优化

随机推荐

  1. PX(像素)转换工具类
  2. Android控件之文本控件---TextView 两种
  3. 学习Android(安卓)Studio里的Gradle
  4. 设置自定义ProgressBar样式
  5. Android(安卓)布局单位转换
  6. Android开发必看知识,不看后悔
  7. 怎样将Android(安卓)源码导入到Android_s
  8. Android中的task作用和Activity启动模式
  9. Google 官方应用架构的最佳实践指南
  10. Android之控件保持在软键盘上面