Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6193259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:05:16+00:00 2026-05-24T03:05:16+00:00

I have a background service which sets a repeating alarm, does its task and

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:05:16+00:00Added an answer on May 24, 2026 at 3:05 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a background Service which must run permanently. The service just has to
I want to have a background service running which gets started when an Image
I have a background service running which sends out emails to users of my
I have a background service which is running all the times and collects sensor
I have a service that has its own thread running on background. I'd like
I have background service which access my SQL Server database. My background service working
First, let me start with some background: I have a web service which accepts
Background I have made a Web Service in Visual Studio, and I'm trying to
Background: I have a module which declares a number of instance methods module UsefulThings
Here is my situation. I have written a WCF service which calls into one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.