您的位置:首页 > 其它

如何停止Handler的消息队列

2017-06-28 11:36 197 查看

停止Handler的消息队列

遇到的问题

调用Handler的
mHandler.removeMessages(SET_VIDEO_PLAYING_CALLBACK);
移除一个消息队列,但是hanlder消息还在轮询发送

Handler接收消息代码如下

`@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
int code = msg.what;
switch (code) {
case SET_VIDEO_PLAYING_CALLBACK:
if (onVideoPlayingCallBack == null) {
return;
}
if (mController != null) {
long currentPos = mController.getCurrentPosition();
long mDuration = mController.getDuration();
int playingStatus = mController.isPlaying() ? 1 : 0;
if (mController.isPlaying()) {
onVideoPlayingCallBack.onVideoPlaying(currentPos, mDuration, playingStatus);
} else {
onVideoPlayingCallBack.onVideoPlaying(currentPos, mDuration, playingStatus);
}
sendemptymessagedelayed(set_video_playing_callback, mplayingcallbackspace);
}
break;
}
super.handleMessage(msg);
}
};`


解决方法:这段代码
if (onVideoPlayingCallBack == null) { return;}


停止的方案
if (mHandler != null && mVideoPlayClick != null) {

mHandler.removeMessages(SET_VIDEO_PLAYING_CALLBACK);

}

this.onVideoPlayingCallBack = null;


mHandler.removeMessages(SET_VIDEO_PLAYING_CALLBACK);
并没有使消息队列销毁

所以就可以这样解决,当
this.onVideoPlayingCallBack = null
时就 return; 这样便不在往下走了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: