The activity lifecycle can detect a clean finish from an "unexpected" termination through use of the isFinishing() function in the onDestroy() callback. You could add this to each activity in your app:
protected void onDestroy () {
boolean crashedOnLastClose = false;
if (!isFinishing()) {
// The application is being destroyed by the O/S
crashedOnLastClose = true;
// Store the result somewhere for the BroadcastReceiver to check later
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("crashedOnLastClose", crashedOnLastClose);
editor.commit();
}
}
Then in your BroadcastReceiver, pull the result back from the SharedPreferences framework (or wherever you decide to store it), and cancel the action if the value is true:
public class MyAlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean crashedOnLastClose = settings.getBoolean("crashedOnLastClose", false);
if (!crashedOnLastClose) {
// No crash on last run, handle the alarm
// ...
}
}
}
Remember to reset it back to false on next launch though! Something like this should do:
protected void onCreate (Bundle savedInstanceState) {
// Make sure this always resets to FALSE on launch
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("crashedOnLastClose", false);
editor.commit();
}
This should capture your crash events; but remember it will also detect other times when your app is force-closed - such as if the memory is low on the device. To distinguish between these kinds of events you would need to inspect the system status as well as isFinishing().
EDIT - Services
Your Service has a separate lifecycle, you have to treat it almost like a separate app. As you've noticed, there is no "isFinishing()" here, but Services are actually quite simple since they are only terminated cleanly by your code, which you can trap pretty easily.
In your Service, add a new boolean (perhaps called "isFinishing") and set it to true when your service is finished with. Then override onDestroy() as well and add a check similar to the one I described for your activities:
protected void onDestroy () {
// Store the result somewhere for the BroadcastReceiver to check later
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("crashedOnLastClose", !isFinishing);
editor.commit();
}
The broadcast receiver will then use the same code we added earlier to detect if either the service or an activity crashed.