温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Android中使用Handler删除Message

发布时间:2020-11-24 16:32:59 来源:亿速云 阅读:933 作者:Leah 栏目:移动开发

今天就跟大家聊聊有关如何在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);<span >         </span> 
<span ></span><pre name="code" class="java"><span >            </span>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. 在子线程中执行完

<span></span><pre name="code" class="java"><span > 
   </span>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有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI