向广播接收器发送新的意图可以为之前的意图提供额外的价值

发布于 2024-10-15 09:52:13 字数 1001 浏览 7 评论 0原文

我正在广播一个意图,该意图将由广播接收器接收,因为应用程序仍在运行,并且警报服务触发了新意图,但接收器显示了先前的意图值。根据文档,广播接收器在返回 onReceive() 后不再处于活动状态,因此接收器应该显示由警报服务触发的下一个意图值,但它没有发生,任何人都可以告诉正确的方法。

这是从活动到广播意图:

 Intent intent = new Intent(SCH_ALARM_ACTION);
    intent.setClass(getBaseContext(), SchAlarmReciever.class);
    intent.putExtra("id", maxId);
    PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(),
                0,
                intent,
                0);
   alarmManager.set(AlarmManager.RTC, gc.getTimeInMillis(), pi);

这是广泛的接收器:

@Override
    public void onReceive(Context context, Intent data) 
                {
        // TODO Auto-generated method stub

        if(data.getAction().equals(SchedulerActivity.SCH_ALARM_ACTION)){

        int id = data.getIntExtra("id",0);
        Toast.makeText(context, "in receiver "+String.valueOf(id), Toast.LENGTH_LONG).show();
                }

这里的 toast 显示了由警报服务的第一次广播发送的 id,即使从警报服务触发了第二个意图(第二次警报响起)

I am broadcasting a intent which will be received by a broadcast receiver, as application is still running and new intent is fired by Alarm Service but the receiver is showing the previous intent value. As per docs broadcast receiver is no longer active after returning onReceive(), so receiver should show next intent values which is fired by alarm service, but it is not happening, can any one tell correct approach.

This is from activity to broadcast intent:

 Intent intent = new Intent(SCH_ALARM_ACTION);
    intent.setClass(getBaseContext(), SchAlarmReciever.class);
    intent.putExtra("id", maxId);
    PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(),
                0,
                intent,
                0);
   alarmManager.set(AlarmManager.RTC, gc.getTimeInMillis(), pi);

This is broadreceiver:

@Override
    public void onReceive(Context context, Intent data) 
                {
        // TODO Auto-generated method stub

        if(data.getAction().equals(SchedulerActivity.SCH_ALARM_ACTION)){

        int id = data.getIntExtra("id",0);
        Toast.makeText(context, "in receiver "+String.valueOf(id), Toast.LENGTH_LONG).show();
                }

here toast shows id which is sent by first broadcast from alarmservice even when second intent is fired from alarmservice(second time alarm goes off)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃兔兔 2024-10-22 09:52:13

得到答案,必须在为 AlarmService 设置挂起意图时设置标志 PendingIntent.FLAG_CANCEL_CURRENT...

Got answer, has to set flag PendingIntent.FLAG_CANCEL_CURRENT while setting pending intent for AlarmService...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文