Android广播接收器接收到过时的消息
创始人
2024-10-08 03:01:24
0

在BroadcastReceiver中加入时间判断,判断接收到的消息是否是过时的消息。

示例代码如下:

public class MyReceiver extends BroadcastReceiver {
    
    private static final String ACTION = "com.example.ACTION_MSG_RECEIVED";
    private static final long VALID_TIME = 60 * 1000; // 有效时间为1分钟
    
    @Override
    public void onReceive(Context context, Intent intent) {
        long receiveTime = System.currentTimeMillis();
        
        if (ACTION.equals(intent.getAction())) {
            long sendTime = intent.getLongExtra("send_time", 0);
            if (sendTime > 0 && receiveTime - sendTime <= VALID_TIME) {
                // 消息在有效时间内,进行处理
                String msg = intent.getStringExtra("msg");
                // TODO: 处理消息
                
            } else {
                // 过时的消息,忽略
            }
        }
    }
}

在发送广播时,需要将消息发送时间一并附带在Intent中:

Intent intent = new Intent();
intent.setAction("com.example.ACTION_MSG_RECEIVED");
intent.putExtra("send_time", System.currentTimeMillis());
intent.putExtra("msg", "Hello, BroadcastReceiver!");
sendBroadcast(intent);

在接收到广播时,首先获取当前时间receiveTime,然后从Intent中取出消息发送时间sendTime,判断sendTime是否在有效时间内(本例中为1分钟),如果是则进行消息处理,否则忽略该消息。

相关内容

热门资讯

透视透视!hhpoker透视功... 透视透视!hhpoker透视功能如何下载,wepoker游戏的安装教程,解密教程(原生是有挂)需要回...
透视好友房!wepoker辅助... 透视好友房!wepoker辅助下载,wepoker有机器人吗,透视教程(先前有挂);wepoker辅...
透视讲解!wepoker作弊辅... 1、透视讲解!wepoker作弊辅助,wpk辅助哪里买,力荐教程(本然有挂);详细教程。2、wepo...
辅助透视!hhpoker透视脚... 辅助透视!hhpoker透视脚本视频,hhpoker俱乐部是干嘛的,解说技巧(好像真的有挂);1分钟...
透视好友房!hhpoker辅助... 透视好友房!hhpoker辅助软件,wepoker免费脚本弱密码,总结教程(其实是真的有挂)1、不需...
透视软件!wepoker辅助器... 透视软件!wepoker辅助器是真的的吗,aapoker辅助软件合法吗,软件教程(竟然是有挂);亲真...
透视最新!德普之星辅助器,hh... 透视最新!德普之星辅助器,hhpoker辅助挂,辅助教程(从来是真的有挂)是一款可以让一直输的玩家,...
透视安卓版!hhpoker透视... 1、透视安卓版!hhpoker透视下载链接在哪里,werplan外卦神器,切实教程(好像有挂)。2、...
透视新版!wepoker透视脚... WePoker透视辅助版本稳定性对比与推荐‌:透视新版!wepoker透视脚本网页,安装不了wepo...
透视脚本!wpk辅助软件,we... 透视脚本!wpk辅助软件,we-poker有人玩吗,德州论坛(先前是真的有挂)关于wpk辅助软件机制...