向广播接收器发送新的意图可以为之前的意图提供额外的价值
我正在广播一个意图,该意图将由广播接收器接收,因为应用程序仍在运行,并且警报服务触发了新意图,但接收器显示了先前的意图值。根据文档,广播接收器在返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
得到答案,必须在为 AlarmService 设置挂起意图时设置标志 PendingIntent.FLAG_CANCEL_CURRENT...
Got answer, has to set flag PendingIntent.FLAG_CANCEL_CURRENT while setting pending intent for AlarmService...