网站建设资讯

NEWS

网站建设资讯

如何在Android中使用Handler删除Message

今天就跟大家聊聊有关如何在Android中使用Handler删除Message,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

成都创新互联公司专注于大通网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供大通营销型网站建设,大通网站制作、大通网页设计、大通网站官网定制、微信平台小程序开发服务,打造大通网络公司原创品牌,更为您提供大通网站排名全网营销落地服务。

Android Handler移除Message详解

问题:

1.removeMessage(what)函数是否只能移除对应what值的Message?

2.对于Delayed发送的Message,能否提前remove?

代码测试:

package javine.k.testhandler; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.HandlerThread; 
import android.os.Message; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
 
public class TestHandlerActivity extends Activity implements OnClickListener { 
 
  private Button startBtn; 
  private Button endBtn; 
  public Handler threadHandler; //子线程Handler 
 
  private Handler mHandler = new Handler() { 
    public void handleMessage(android.os.Message msg) { 
      threadHandler.sendEmptyMessageDelayed(1, 2000); 
      Log.d("info", "handle main-thread message..."); 
    }; 
  }; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
 
    startBtn = (Button) findViewById(R.id.startButton); 
    endBtn = (Button) findViewById(R.id.endButton); 
    startBtn.setOnClickListener(this); 
    endBtn.setOnClickListener(this); 
 
    new Thread(new Runnable() { 
      @Override 
      public void run() { 
        HandlerThread handlerThread = new HandlerThread("handler"); 
        handlerThread.start(); 
        threadHandler = new Handler(handlerThread.getLooper()) { 
          @Override 
          public void handleMessage(Message msg) { 
            //mHandler.sendEmptyMessageDelayed(0, 2000);          
            mHandler.sendEmptyMessageDelayed(1, 2000); 
Log.d("info", "handle sub-thread message...");}};}}).start();}@Overridepublic void onClick(View v) 
{// TODO Auto-generated method stubswitch (v.getId()) {case R.id.startButton: 
//开始发送消息mHandler.sendEmptyMessage(1);break;case R.id.endButton:
//移除主线程Handler的消息mHandler.removeMessages(1);break;default:break;}}}

测试结果:

        1. removeMassage(1)无法移除what=0的Message。

2. 在子线程中执行完

 
   mHandler.sendEmptyMessageDelayed(1, 2000); 
Log.d("info", "handle sub-thread message...");

  之后,即可通过removeMesage(1)来移除消息,mHandler将不能接收到该条消息。

源码分析:

1.Android如何移除一条Message?

查看源码可知,Handler.removeMessage(int what)内部调用MessageQueue.removeMessage(this, what, null)

查看MessageQueue的removeMessage方法如下:

void removeMessages(Handler h, int what, Object object) { 
    if (h == null) { 
      return; 
    } 
 
    synchronized (this) { 
      Message p = mMessages; 
 
      // Remove all messages at front. 
      while (p != null && p.target == h && p.what == what 
         && (object == null || p.obj == object)) { 
        Message n = p.next; 
        mMessages = n; 
        p.recycle(); 
        p = n; 
      } 
 
      // Remove all messages after front. 
      while (p != null) { 
        Message n = p.next; 
        if (n != null) { 
          if (n.target == h && n.what == what 
            && (object == null || n.obj == object)) { 
            Message nn = n.next; 
            n.recycle(); 
            p.next = nn; 
            continue; 
          } 
        } 
        p = n; 
      } 
    } 
  } 

筛选要移除的Message的条件是:target(handler),what,object

该函数分两步来移除Message:

1).移除在前端的符合条件的Message

2).移除后面的符合条件的Message

2.为何延迟发送的Message在延迟时间到达之前就可以被移除?

Handler.sendEmptyMessageDelayed() ---调用---> sendMessageAtTime() -----调用---> enqueueMessage() ----调用MessageQueue.enqueueMessage()

实际进行处理的就是MessageQueue,源码如下:

boolean enqueueMessage(Message msg, long when) { 
    if (msg.isInUse()) { 
      throw new AndroidRuntimeException(msg + " This message is already in use."); 
    } 
    if (msg.target == null) { 
      throw new AndroidRuntimeException("Message must have a target."); 
    } 
 
    synchronized (this) { 
      if (mQuitting) { 
        RuntimeException e = new RuntimeException( 
            msg.target + " sending message to a Handler on a dead thread"); 
        Log.w("MessageQueue", e.getMessage(), e); 
        return false; 
      } 
 
      msg.when = when; 
      Message p = mMessages; 
      boolean needWake; 
      if (p == null || when == 0 || when < p.when) { 
        // New head, wake up the event queue if blocked. 
        msg.next = p; 
        mMessages = msg; 
        needWake = mBlocked; 
      } else { 
        // Inserted within the middle of the queue. Usually we don't have to wake 
        // up the event queue unless there is a barrier at the head of the queue 
        // and the message is the earliest asynchronous message in the queue. 
        needWake = mBlocked && p.target == null && msg.isAsynchronous(); 
        Message prev; 
        for (;;) { 
          prev = p; 
          p = p.next; 
          if (p == null || when < p.when) { 
            break; 
          } 
          if (needWake && p.isAsynchronous()) { 
            needWake = false; 
          } 
        } 
        msg.next = p; // invariant: p == prev.next 
        prev.next = msg; 
      } 
 
      // We can assume mPtr != 0 because mQuitting is false. 
      if (needWake) { 
        nativeWake(mPtr); 
      } 
    } 
    return true; 
  } 

由上可知:MessageQueue会对需要延迟发送的Message排序,按照需要延迟的时间长短(when)。

即,虽然是延迟发送的消息,其实当你调用发送函数之后,Message就已经被添加到MessageQueue中去了。

看完上述内容,你们对如何在Android中使用Handler删除Message有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


分享题目:如何在Android中使用Handler删除Message
URL地址:http://njwzjz.com/article/ppepei.html