-1

In my app, I want to have one user logged in at a time. Hence, when they log out I am making an API call to make the user offline in my server also.

But in case the user is logged in and erases the app data or uninstall the app how do I notify my server of the same. How can I listen to uninstall event and do some pre-uninstalling tasks.

Because if I cannot do so, next time when the same user installs the app and tries to login, he will not be able to do so because his current status on my server would be offline because log out never happened.

Thanks

  • Do you mean the user is logged in only while the app is open, or constantly? – nkorth Jul 06 '15 at 13:29
  • possible duplicate of [Is it possible to detect Android app uninstall?](http://stackoverflow.com/questions/6209730/is-it-possible-to-detect-android-app-uninstall) – ligi Jul 06 '15 at 13:31
  • No, he can be logged in even if he is out of the app. The thing is if he is logged in and has unistalled the app or has erased the data, in that case I want to hear for that uninstall event so that I can do something to update my server. So that next time, when the user installs the app, gets logged in successfully. – Alok Singh Jul 06 '15 at 13:34
  • 1
    This is something you most likely won't be able to detect reliably. What if a user loses their phone - the uninstall trigger won't have happened, and they won't be able to log in on a new device (if I'm understanding you correctly) – nkorth Jul 06 '15 at 13:37
  • yes, but that is the worst case. – Alok Singh Jul 06 '15 at 13:40

2 Answers2

1

How can I listen to uninstall event and do some pre-uninstalling tasks.

You don't.

because his current status on my server would be offline because log out never happened

Then your problem is on your server. Your server needs to implement an inactivity timeout, that reverts the state to "logged out" if the user has not been seen in some time. There also needs to be some means for the user to re-authenticate and re-connect to an existing "logged in" state, as the user may have more than one device (e.g., a phone and a tablet) and want to work with your app and server on both of them.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Here's my best idea. It might solve your problem, though not in the particular way you wanted:

Add a Service to your app which starts on device boot. Send the user login to your database in this service's onCreate, and send the logout in its onDestroy. This will result in almost the same behavior you explained, with the addition of logging the user out whenever their phone is completely shut off.

(I'm not saying any of this is a good idea, but if I had to, that's how I would do it.)

nkorth
  • 1,684
  • 1
  • 12
  • 28