0

I have a data object which occasionally gets refreshed from a data source on the web using a recurring alarm.

This data object has keeps track of objects which want to be notified when it gets new data. I currently use two means to register listeners - they can register either as a callback if they expect to be refreshed only while the app is in the foreground, or they can register with a PendingIntent if the observing object needs to be notified even if the app is not active.

My problem is that if my data object gets removed from memory, then its list of PendingIntents is also removed. I suppose I could come up with a complicated method of persisting the list and all of the fields in it in shared preferences or something, but I think that there just has to be a better way.

Is there a way that I can ask the system, which I know holds its own list of PendingIntents, for all of "my" PendingIntents?

Note that I cannot re-create the PendingIntents when I create the object or at any other time - the are created by the listeners, and there is no way to deduce what fields they have set or their values.

rothloup
  • 1,220
  • 11
  • 29
  • "I suppose I could come up with a complicated method of persisting the list and all of the fields in it in shared preferences or something" -- nope. "the are created by the listeners, and there is no way to deduce what fields they have set or their values" -- if these listeners are in your app, why are you using `PendingIntents` in the first place? If these listeners are not in your app, then they are not your `PendingIntents` (with respect to "for all of 'my' `PendingIntents`"). – CommonsWare Jan 25 '17 at 15:54
  • @CommonsWare: My intention is to use the PendingIntent to notify my other app components to update their displays or whatever they want to do when my data object refreshes itself. If the user is not interacting with the app at the time, what other method do I have of notifying my app components that there is new data available? In this particular case, I'm updating things like appWidgets which display the data and checking if notifications should be issued. – rothloup Jan 25 '17 at 16:19
  • "what other method do I have of notifying my app components that there is new data available?" -- um, call methods on Java objects? If your concerns are tight coupling, use an event bus. "I'm updating things like appWidgets which display the data and checking if notifications should be issued" -- use `AppWidgetManager` to update the app widget. No `PendingIntent` is required to update an app widget. – CommonsWare Jan 25 '17 at 16:24
  • @CommonsWare: You seem to misunderstand my use case. The data object has no idea which components are going to need updates, so I can't just list them out and make calls. I can register listeners and makes calls on them while the app is in memory, but when it is removed by the system then my list of listeners is gone. That's what motivated me to use PendingIntents. – rothloup Jan 25 '17 at 16:32
  • "The data object has no idea which components are going to need updates, so I can't just list them out and make calls" -- use an event bus or RxJava. "That's what motivated me to use PendingIntents" -- AFAIK, a `PendingIntent` is reference-counted and will be removed once nothing is using it. Plus, you cannot get "your" `PendingIntents` back, except by recreating them. – CommonsWare Jan 25 '17 at 16:48

2 Answers2

0

Have a look at these Storage Options.

Previously I have had to schedule alarms using pending intents. When the device is rebooted, the OS removes all of its alarms, so I had to recreate them when the OS had finished booting.

To determine if two PendingIntents match, the following must be equal:

  • The requestCode parameter used when the PendingIntent was created
  • The Intent ACTION
  • The Intent CATEGORIES
  • The Intent DATA
  • The Intent MIMETYPE
  • The Intent PACKAGE
  • The Intent COMPONENT

Extras are not taken into consideration.

So I stored this information into a RelationalDatabase (Realm in my case) and could 'recreate' the pending intents at a later date.

In addition, have a look at this answer. It is very similar to yours.

Community
  • 1
  • 1
Charlie
  • 2,876
  • 19
  • 26
0

Sounds complicated and probably not possible (using PendingIntent).

Why don't you just broadcast changes to your objects in a broadcast Intent. That way you don't have to keep track of any listeners. Any component that wants to listen for changes in the object can just register a listener for the appropriate broadcast Intent.

David Wasser
  • 93,459
  • 16
  • 209
  • 274