Intent extras not removed/replaced

2019-05-16 08:39发布

I am setting an alarm on a button click.

The alarm is triggered with an intent.

This intent get an extra "int" to pass to the Broadcast Receiver.

The problem is that the intent's extra gets set once on the first click of the button and never changes on the other clicks:

Intent intent = new Intent(A.this, B.class);
intent.putExtra(WAKEUP_DURATION, wakeUpDuration);
PendingIntent sender = PendingIntent.getBroadcast(A.this, 0, intent, 0);

I tried removing it in the Broadcast Receiver, but no luck:

intent.removeExtra(A.WAKEUP_DURATION);

2条回答
可以哭但决不认输i
2楼-- · 2019-05-16 09:18

Thanks! That did the trick. For those of you who would like to know the exact answer. The "FLAG_UPDATE_CURRENT" goes as the fourth argument in the method 'getBroadcast'. It should look like this:

PendingIntent sender = PendingIntent.getBroadcast(A.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
查看更多
再贱就再见
3楼-- · 2019-05-16 09:21

Use FLAG_UPDATE_CURRENT when creating your PendingIntent to update the extras from a new Intent.

查看更多
登录 后发表回答