I am writing some code that can migrate some device persisted data to a network server when the device/apps C2DM registration token changes.
This is ultimately kicked off by a broadcastreceiver (specifically a receiver called as the result of receiving a Google C2DM registration token)
@Override
public void onRegistered(Context context, String registrationId) throws IOException {
Log.d(TAG, "registrationId: " + registrationId);
Intent selectIntent = new Intent(this, AlertMigrationService.class);
this.startService(selectIntent);
}
(Note: My app uses a custom application class that extends the normal Application class.)
I think technically this receiver could be called when my application is in the background (or not running) – so what happens when my intent service is started? Does it spool up my entire application and create an instance of my Application class?
Is it safe (or even a good idea) for me to call static methods on my Application class from inside the IntentService? (These static methods have return objects initialized in Application.onCreate)
When your IntentService starts, it will create an instance of your Application class, if your application is not already running. If your application is alive, you will be able to access modified application-specific objects as they are. But if your application was killed and then the br starts the service , a new instance of the whole application is created. The static methods wont give you the values you desire at that time. Persist the data somewhere and don’t depend on the application lifecycle is what I suggest.