0

I'm working with altbeacon library how can I add the beacon details to database when the app is being killed from background.

1 Answers1

0

On Android, there is no guaranteed callback when your app is killed. See a discussion of this here.

You might have success getting a callback with a shutdown hook:

Runtime r=Runtime.getRuntime();  
r.addShutdownHook(new MyThread());  

But I suspect that if and when it fires, the Android Context will be invalid making it impossible to write to your database.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Thank you davidgyoung. But i need to get the beacon details when the app is being killed from the background. Where will i make a call to application class extending BootstrapNotifier? – JINCY VP Feb 19 '19 at 14:31
  • I don't think that is possible with Android. Alternative: store the beacon/time every time one is detected (e.g. every second or so), and then when the app starts up again, check the last stored beacon timestamp to see if it more than a few seconds in the past. If it is, this indicates that your app was previously stopped: mark it in the database as needed. This would meet your requirements except perhaps that the storage in the database would not happen exactly when the app is killed, but it would do this on next app launch based on the info available at the time the app was last killed. – davidgyoung Feb 19 '19 at 15:40
  • Thank you davidgyoung. But i can't get beacon details when app is being killed.Could you help with that? how to use RegionBootstrap to scan beacon in background? Where to add the call to application class extending BootstrapNotifier in the application? – JINCY VP Feb 20 '19 at 04:37
  • There are code samples on how to do that in the "Starting an app in the background" section here: https://altbeacon.github.io/android-beacon-library/samples.html – davidgyoung Feb 20 '19 at 04:54