I'm working with altbeacon library how can I add the beacon details to database when the app is being killed from background.
Asked
Active
Viewed 105 times
0
-
when app killed, onDestroy method called, at that time add beacon entry into the database. – Hemant N. Karmur Feb 19 '19 at 08:57
-
Thank you for the reply Hemant N. Karmur – JINCY VP Feb 19 '19 at 10:02
-
I am following sample in https://altbeacon.github.io/android-beacon-library/samples.html to detect beacon in background. Inside didEnterRegion method i gave inserion to database. I called this class inside on destroy of main activity... Is this correct? Can anyone help? – JINCY VP Feb 19 '19 at 12:23
-
where you want to save your beacons details? – Hemant N. Karmur Feb 19 '19 at 12:27
-
In sqlite database. – JINCY VP Feb 19 '19 at 12:41
-
Where to add call for application class extending region bootstrap? – JINCY VP Feb 19 '19 at 13:00
1 Answers
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