I have an application with login/logout that uses android’s Accounts. I’m using GCM too, so when the user logs in I register the device and if the user logs out I unregister it. I’m facing a problem with the unregister call.
To unregister my device from my server I need the account’s token. Since I can’t pass it to the onUnregistered method from GCMIntentService (or at least I don’t know how), I tried to get it using AccountManager, but at that point my account is already deleted. I want to solve it, and two options came to my mind:
1- Call first GCMRegistrar.unregister and delete the account in the GCMIntentService->onUnregistered function. The problem with this solution is that the user has to wait for unregister to finish, and maybe it doesn’t. I can’t let the user go on without deleting the account.
2- Save the token somewhere (application class maybe) to be able to get it from GCMIntentService->onUnregistered.
I think the number 2 is better, but I don’t know if it’s the best choice. Is there any better way to solve it?
Once you get the token in the IntentService you can save it with SharedPreferences:
And then read it from everywhere in your app: