sendEmptyMessageAtTime和sendEmptyMessageDelayed的区别

场景:想实现一个每隔1s就循环发送消息的功能,没注意用了sendEmptyMessageAtTime,结果程序跑了一会就挂了,没道理呀!

发现消息

 mHandler.sendEmptyMessageAtTime(MSG_UPDATA_PROGRESS,MSG_UPDATA_DELAY);

收到之后再次发送消息

   private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //更新播放进度
            if (msg.what==MSG_UPDATA_PROGRESS){
                // 获得当前播放时间和当前视频的长度
                int currentPosition = mVideoView.getCurrentPosition();
                int duration = mVideoView.getDuration();
                int time = ((currentPosition * 100) / duration);
                // 设置进度条的主要进度,表示当前的播放时间
                mSeekBar.setProgress(time);
                mHandler.sendEmptyMessageAtTime(MSG_UPDATA_PROGRESS,MSG_UPDATA_DELAY); //循环发送崩溃

            }
            super.handleMessage(msg);
        }
    };

仔细研究二者的区别才发现问题所在:sendEmptyMessageAtTime是在指定时间发送,也就是说你循环调用,但是最终还是在同一时刻发送,所以最终的结果是导致大量的消息在同一时刻被发送和处理,当然就挂了。

  • sendEmptyMessageAtTime源码
    /**
     * Sends a Message containing only the what value, to be delivered 
     * at a specific time.
     * @see #sendMessageAtTime(android.os.Message, long)
     *  
     * @return Returns true if the message was successfully placed in to the 
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.
     */

    public final boolean sendEmptyMessageAtTime(int what, long uptimeMillis) {
        Message msg = Message.obtain();
        msg.what = what;
        return sendMessageAtTime(msg, uptimeMillis);
    }

其中

Sends a Message containing only the what value, to be delivered 
at a specific time
 ```

说的很清楚,在特定的时间MSG_UPDATA_DELAY也就是1s被发送


######源码

 /**
 * Sends a Message containing only the what value, to be delivered
 * after the specified amount of time elapses.
 * @see #sendMessageDelayed(android.os.Message, long) 
 * 
 * @return Returns true if the message was successfully placed in to the 
 *         message queue.  Returns false on failure, usually because the
 *         looper processing the message queue is exiting.
 */
public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
    Message msg = Message.obtain();
    msg.what = what;
    return sendMessageDelayed(msg, delayMillis);
}
说的是
```java
Sends a Message containing only the what value, to be delivered 
after the specified amount of time elapses.




<div class="se-preview-section-delimiter"></div>

指定时间之后才发送,也就是说我隔1s再发送,收到之后再隔1s再发送。

最后解决办法:把mHandler.sendEmptyMessageAtTime(MSG_UPDATA_PROGRESS,MSG_UPDATA_DELAY);
改成 mHandler.sendEmptyMessageDelayed(MSG_UPDATA_PROGRESS,MSG_UPDATA_DELAY);
就好了。

总结:
1.sendEmptyMessageAtTime(what, inMillis)是在固定时间发送消息,其中第二个参数是已开机时间为准,可能你开机时间到现在已经超过了5S,所以就立即执行了

2.sendMessageAtTime的uptimeMillis是相对系统开机时间的绝对时间,SystemClock.uptimeMillis()是当前开机时间。

3.这两句是等效的,都是延时1秒将消息加入列队

msgHandle.sendMessageAtTime(msg, SystemClock.uptimeMillis()+1000);
msgHandle.sendMessageDelayed(msg, 1000)

如果本文对你有帮助,就关注下作者吧点此查看全部最新文章


博客CSDN
我的简书
我的GitHub,喜欢的话给个star吧

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员学园

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值