Esempio n. 1
0
    @Override
    public void run() {
      Looper.prepare();
      Looper myLooper = Looper.myLooper();
      Looper mainLooper = Looper.getMainLooper();

      String msgobj;

      if (null == myLooper) {
        mOwnLooperThreadHandler = new EventHandler(mainLooper);
        msgobj =
            "OwnLooperThread has no looper and handleMessage function executed in main thread!";
      } else {
        mOwnLooperThreadHandler = new EventHandler(myLooper);
        msgobj =
            "This is from OwnLooperThread self and handleMessage function executed in NoLooperThread!";
      }

      mOwnLooperThreadHandler.removeMessages(0);

      // 给自己发送消息
      Message msg = mOwnLooperThreadHandler.obtainMessage(3, 1, 1, msgobj);
      mOwnLooperThreadHandler.sendMessage(msg);
      Looper.loop();
    }
Esempio n. 2
0
 @Override
 public void run() {
   Looper myLooper = Looper.myLooper();
   Looper mainLooper = Looper.getMainLooper();
   String msgobj;
   if (null == myLooper) {
     // 这里获得的是主线程的Looper,由于NoLooperThread没有自己的looper所以这里肯定会被执行
     mNoLooperThreadHandler = new EventHandler(mainLooper);
     msgobj = "NoLooperThread has no looper and handleMessage function executed in main thread!";
   } else {
     mNoLooperThreadHandler = new EventHandler(myLooper);
     msgobj =
         "This is from NoLooperThread self and handleMessage function executed in NoLooperThread!";
   }
   mNoLooperThreadHandler.removeMessages(0);
   if (bpostRunnable == false) {
     // send message to main thread
     Message msg = mNoLooperThreadHandler.obtainMessage(2, 1, 1, msgobj);
     mNoLooperThreadHandler.sendMessage(msg);
     Log.e(TAG, "NoLooperThread id:--------+>" + this.getId());
   } else {
     // 下面new出来的实现了Runnable接口的对象中run函数是在Main Thread中执行,不是在NoLooperThread中执行 记得 null == myLooper么
     // 注意Runnable是一个接口,它里面的run函数被执行时不会再新建一个线程
     // 您可以在run上加断点然后在eclipse调试中看它在哪个线程中执行
     mNoLooperThreadHandler.post(
         new Runnable() {
           public void run() {
             // TODO Auto-generated method stub
             tv.setText("update UI through handler post runnalbe mechanism!");
             Log.e(TAG, "update UI id:--------+>" + Thread.currentThread().getId());
             noLooperThread.stop();
           }
         });
   }
 }
Esempio n. 3
0
  public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
      case 101:
        // 主线程发送消息给自己
        Looper looper = Looper.myLooper(); // get the Main looper related with the main thread
        // 如果不给任何参数的话会用当前线程对应的Looper(这里就是Main Looper)为Handler里面的成员mLooper赋值
        mHandler = new EventHandler(looper);
        // 清除整个MessageQueue里的消息
        mHandler.removeMessages(0);
        String obj = "This main thread's message and received by itself!";

        Message msg = mHandler.obtainMessage(1, 1, 1, obj);
        // 将Message对象送入到main thread的MessageQueue里面
        mHandler.sendMessage(msg);
        break;
      case 102:
        // other线程发送消息给主线程
        bpostRunnable = false;
        noLooperThread = new NoLooperThread();
        noLooperThread.start();
        break;
      case 103:
        // other thread获取它自己发送的消息
        tv.setText("please look at the error level log for other thread received message");
        ownLooperThread = new OwnLooperThread();
        ownLooperThread.start();
        break;
      case 104:
        // other thread通过Post Runnable方式发送消息给主线程
        bpostRunnable = true;
        noLooperThread = new NoLooperThread();
        noLooperThread.start();
        break;
      case 105:
        // 主线程发送消息给other thread
        if (null != mOtherThreadHandler) {
          tv.setText(
              "please look at the error level log for other thread received message from main thread");
          String msgObj = "message from mainThread";
          Message mainThreadMsg = mOtherThreadHandler.obtainMessage(1, 1, 1, msgObj);
          mOtherThreadHandler.sendMessage(mainThreadMsg);
        }
        break;
      case 106:
        finish();
        break;
    }
  }