2

I have a background service which sets a repeating alarm, does its task and stops itself. Then when the alarm wakes up it starts the service again. If the program crashes the alarm is still around and wakes up the alarm broadcastreceiver. Is there any way to cancel the alarm on a crash - I suppose I might be able to cancel the alarm from any caught exceptions but what about other causes?

Or when the alarm Broadcast receiver is triggered is there any way to tell if the program that set it has crashed?

ron
  • 5,219
  • 8
  • 39
  • 44

1 Answers1

3

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.

seanhodges
  • 17,426
  • 15
  • 71
  • 93
  • Nice. The drawbacks I see is that every activity/service should add this code. And this approach is not applicable for ContentProviders. – inazaruk Jul 25 '11 at 16:15
  • Thanks sean I'll try that. Between alarms would the app be available for force close due to low memory? If it is and does get closed would the alarm wake it up again or does low memory closing destroy the alarm broadcast receiver? I've wondered what mechanism makes the alarm broadcastreceiver available - assumed details were kept in the underlying os so as to do a start up. – ron Jul 25 '11 at 16:24
  • @inazaruk some good points. People often share code between activities by extending a shared abstract class, so for some the duplication would be straightforward. Something else would probably be needed to analyse a ContentProvider lifecycle. – seanhodges Jul 26 '11 at 09:57
  • @ron your app will be eligible for termination only while it is running, regardless of how your alarms are scheduled. The AlarmProvider source code acts like a global scheduler and will start your app if it is in a "stopped" state to execute your BroadcastReceiver. – seanhodges Jul 26 '11 at 10:14
  • @Sean as well as using isFinishing() in onDestroy() in two activities I need to use it in a service. isFinishing is ok in the activites but I cant work out how to code it in the service. I tried :- – ron Aug 01 '11 at 15:57
  • I tried :- if (!((Activity) getBaseContext()).isFinishing()) { and whilst it compiled ok it errored when running with – ron Aug 01 '11 at 15:58
  • (why is carriage return dispatching this message when i'm not finished) – ron Aug 01 '11 at 15:59
  • @ron I've updated my answer to include services. Your error was because the base Context of a Service cannot be cast to an Activity, they are different beasts. – seanhodges Aug 02 '11 at 09:11
  • Sean - thanks for all your help. I've forced a runtime crash in my main activity code and included output to a log file from onDestroy and it seems that on the crash the onDestroy is never entered. I've also stepped through in Debug and same result. I'm flummoxed!! – ron Aug 03 '11 at 19:13
  • That depends on the nature of your runtime crash, some faults are not recoverable within the standard Android app lifecycle. If you're looking for some sort of "catch-all" approach, you might want to look at this thread: http://stackoverflow.com/questions/4028742/how-to-clear-a-notification-if-activity-crashes – seanhodges Aug 05 '11 at 10:27