在使用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转成字符串来传。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值