9

I use an AlarmManager to start a service. When i set up the AlarmManager i use the PendingIntent and use a unique requestCode which is equal to a id row in my database.

PendingIntent pendingIntent = PendingIntent.getBroadcast(SettingsActivity.this,
                            lecture.getId(), myIntent, 0); 

How can I retrieve that id in my service when it starts? I basically need only the requestCode parameter. I want to use that id to retrieve data from my database and show it in a notification. I have implemented all the stuff, I just need that requestCode. Is it possible to get it?

Thanks

Aksiom
  • 1,565
  • 4
  • 25
  • 39

1 Answers1

15

You need to put lecture.getId() into extras of your myIntent. According to Javadoc requestCode is not even used yet.

// store id
myIntent.putExtra("id", lecture.getId());

// read id or -1, if there is no such extra in intent
int id = myIntent.getIntExtra("id", -1);
sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
  • But I need that id even when the app is killed. When an app is killed the extra data is deleted. How can i get the necessary data from the database on the service when the id in the extras will be null? Or am I wrong on this? – Aksiom Aug 22 '13 at 10:58
  • 1
    @Aksiom the `PendingIntent` when got fired will have the extra, it has nothing to do with process getting killed, `PendingIntent` is sort of intent which run independently from the app, even if your app is not running!! – Muhammad Babar Aug 22 '13 at 11:03
  • 1
    @Aksiom but when device is booted(restarted) `PendingIntent` will get removed so **on boot** you have to re-register the `PendingIntent` with the `AlarmManager` – Muhammad Babar Aug 22 '13 at 11:05
  • `AlarmManager` will keep `Intent` for you, even if your app is stopped. Once there is alarm time, `AlarmManager` will launch your app again and send your intent to your broadcast receiver. No information will be lost. `AlarmManager` removes the data when your phone is restarted or you cancel the intent only. – sergej shafarenka Aug 22 '13 at 11:05
  • @MuhammadBabar ok thanks for the info. Can you explain how to re-register the `PendingIntent` on device boot? – Aksiom Aug 22 '13 at 14:56
  • @Aksiom check out the link beworker provided! and for more details on `BroadcastReceivers` refer to http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html – Muhammad Babar Aug 23 '13 at 05:08