栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

在使用AlarmManager构造intent,设置定时广播闹钟,在时间到达后,广播接收到的bundle为空

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在使用AlarmManager构造intent,设置定时广播闹钟,在时间到达后,广播接收到的bundle为空

  • 开发过程中,使用AlarmManager时遇到了一个问题至今网上没有明确原因:

    构造intent,设置定时广播闹钟,在时间到达后,广播接收到的bundle为空

    private void setMessageEndTrigger(MessageRecommend requireMessage) {
        Intent startMessage = new Intent(MESSAGE_START_ACTION);
        Bundle bundle = new Bundle();
        bundle.putSerializable(MESSAGE, requireMessage);
        startMessage.putExtras(bundle);
        PendingIntent intent = PendingIntent.getBroadcast(mContext,
                MESSAGE_WHAT, startMessage, PendingIntent.FLAG_CANCEL_CURRENT);
        if (requireMessage == null) {
            LogUtil.debug(TAG + "TimingMessage end is cancel");
            mAlarmManager.cancel(intent);
            return;
        }
        long endTime = TimeParseUtil.parseLong(requireMessage.getEndTime());
        mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, endTime, intent);
    }
    private void checkStartMessage(Intent intent) {
        // rec是空的
        MessageRecommend rec = (MessageRecommend) intent.getExtras().get(MESSAGE);
        if (!mAreaManager.checkArea(rec.getProvinceName(), rec.getCityName())) {
            LogUtil.warning(TAG + "the city is change");
            return;
        }
        if (!mAccountManager.getAccountId().equals(rec.getAccount())) {
            LogUtil.warning(TAG + "the Account is change");
            return;
        }
        sendOperatorMessage(rec);
    }
  • 第一种解决:将bundle使用putExtra方法设置Key和Value,放弃putExtras方法直接put进bundle
    private void setMessageStartTrigger(MessageRecommend requireMessage) {
        Intent startMessage = new Intent(MESSAGE_START_ACTION);
        Bundle bundle = new Bundle();
        bundle.putSerializable(MESSAGE, requireMessage);
        startMessage.putExtra(MESSAGE, bundle);
        PendingIntent intent = PendingIntent.getBroadcast(mContext,
                MESSAGE_WHAT, startMessage, PendingIntent.FLAG_CANCEL_CURRENT);
        if (requireMessage == null) {
            LogUtil.debug(TAG + "TimingMessage start is cancel");
            mAlarmManager.cancel(intent);
            return;
        }
        long startTime = TimeParseUtil.parseLong(requireMessage.getStartTime());
        LogUtil.debug(TAG + "TimingMessage start time" + startTime);
        mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, startTime, intent);
    }
  • 广播接收时使用先去调用getBundleExtra方法获取bundle,在通过bundle的getSerializable方法获取对象
    private void checkStartMessage(Intent intent) {
        MessageRecommend rec = (MessageRecommend) intent.
                getBundleExtra(MESSAGE).getSerializable(MESSAGE);
        if (rec == null) {
            LogUtil.debug(TAG + "start time Message is null ");
            return;
        }
        LogUtil.debug(TAG + "start time Message: " + rec.toString());
//        if (!mAreaManager.checkArea(rec.getProvinceName(), rec.getCityName())) {
//            LogUtil.warning(TAG + "the city is change");
//            return;
//        }
        if (!mAccountManager.getAccountId().equals(rec.getAccount())) {
            LogUtil.warning(TAG + "the Account is change");
            return;
        }
        sendOperatorMessage(rec);
    }
  • 第二种解决:

    可以把传递的参数都转成String传,对象也通过Json转成字符串来传。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632019.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号