主线程给子线程发送消息

//创建主线程的handler

private Handler handler = new Handler(){

  public void handleMessage(Message msg) {

    Message message = new Message();

    //向子线程发消息

    threadHandler .sendMessDelayed(message , 1000);

  }

};

private Handler threadHandler;

onCreate()方法中:

  HandlerThread thread = new HandlerThread("handlerThread");  //HandlerThread解决线程并发问题,可以查看源码。通过它可以获得Looper(Looper不会为空)

  thread .start();

  //创建子线程的handler

  threadHandler = new Handler(thread.getLooper()){

    public void handleMessage(Message msg) {

      Message message = new Message();

      //向主线程发消息

      handler.sendMessDelayed(message , 1000);

    }

  };

posted @ 2015-10-06 13:29  annie-baby  阅读(773)  评论(0编辑  收藏  举报