Value set via Intent.putExtra is not saved
I am writing an application that starts a service in the background. This service receives intents with instructions to do something, and then performs those actions on a different thread. Intent.putExtra()
used to include information about an action to be performed.
If for any reason the action fails, a notification is generated indicating this to the user. The notification includes a "retry" button that can be used to retry a failed action.
This is done as follows, where intent
is the intent that was originally used to start the service:
intent.putExtra(EXTRA_LAST_FAIL, 1234);
new Notification.Builder(context)
...
.addAction(
R.drawable.ic_action_retry,
context.getString(R.string.action_retry),
PendingIntent.getService(context, 0, intent, 0)
)
...
I expect the intent to contain new data ( EXTRA_LAST_FAIL
) when it is delivered to the service, but it doesn't:
int lastFail = intent.getIntExtra(EXTRA_LAST_FAIL, 0);
if (lastFail != 0) {
Log.d(TAG, "action failed last time");
}
However, even though the above value is 1234
above, the resubmitted intent does not contain EXTRA_LAST_FAIL
. getIntExtra()
returns 0.
What am I doing wrong?
source to share
PendingIntent.getService(context, 0, intent, 0)
Replace it like this:
PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
If the previous equivalent PendingIntent
is still considered outstanding, getService()
effectively returns the existing one PendingIntent
. FLAG_UPDATE_CURRENT
will replace additional functions.
source to share