Starting service with an initial value
-
28-10-2019 - |
Question
I am trying to get a reminder/alarm Service` working in my note/todo app. I am able to set a reminder for a particular item and the alarm triggers and displays a notification successfully.
My problem is how can my Service
know which note/todo item set that particular reminder. I'd like for the user to be able to click on the notificaiton in the status bar and have the item which trigger it come up. But I have no way of passing that information to the Service
as they don't accept Bundles
from the PendingIntent
.
I currently set the alarm with the following:
private void createAlarm() {
Intent i = new Intent(this, AlarmService.class);
PendingIntent sender = PendingIntent.getService(this, 0, i, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, mReminderCal.getTimeInMillis(), sender);
}
I just need a way to send along the _id
of the item in my database so my Service can launch the item with that same _id
when the notification is clicked.
I hope my question isn't too confusing.
Thanks!
Solution
Why don't you put all your need into Intent data? Something like this:
final Intent intent = new Intent(context, UpdatesActivity.class);
intent.putExtra(ID, "foo");
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Then on receiving end you do
String id = getIntent().getStringExtra(ID);