Good practice for app that times when it crashes

224 views Asked by At

my app doesn't currently crash BUT we are trying to make it very robust and one thing we are doing is keeping track of the time of "some event" until it is manually stopped by the user.

My Intended Implementation

This is how I was planning on implementing this. I was going to make it so when they submit the initial "event" it start timing and starts the initial time and the rest of the event in our SQLite database. Then a button appears on the same submit screen to STOP timing the event. We want to have accurate time tracking for the event. I would set a flag in preferences to say there is an event that is in progress and another preference with its unique id.

Concern

So my concern is this: if the app crashes or something bad happens, I want to give the person an option to change the end time for the event. I was going to do this in one of two ways: 1. Do a check when the app starts up again (if there is a way to do this) that asks if the flag is set still, and if the app just started up. Then give them this option to edit. 2. If it exists, call an onTerminate method or something that android uses when it closes EVEN if it crashed. Again, I don't know if this exists.

Any suggestions?

1

There are 1 answers

0
Code Droid On BEST ANSWER

Issue a sendStickyBroadcast(Intent) with the state in the broadcast. At any later time register a null receiver for the sticky broadcast and you will have the last known broadcast state. Does not matter if it went down. So you might have one broadcast on startup etc.

and off course when app starts up again is ON_BOOT_COMPLETED intent broadcast which you will want to listen for.

Also DO NOT count on Android calling any particular methods on crash.

The general principal is this: If there is something important to save (ncluding state), do it immediately like issuing stickyBroadcast or store as a SharedPreference. Don't wait for any particular callbacks or crash event handling. There is nothing that cannot be handled by just saving state information immediately and handling ON_BOOT_COMPLETE or checking if first startup value in SharedPreferences. This is really no different than checksums. Starting your event is the beginning of a message. You should have an end of message too. Or end of event event. If you don't see it you did not succeed. Just issue last state frequently, and recover accordingly.