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

  • SEARCH
  • Home
  • 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 7853517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:43:25+00:00 2026-06-02T19:43:25+00:00

I have a fairly basic app that only has two Activities, the splash screen

  • 0

I have a fairly basic app that only has two Activities, the splash screen which goes into the MainActivity. I’ve created a notification with a fixed time that it fires using AlarmManager. Right now I have the AlarmManager execute during the splash screen. I have two issues. One: since the AlarmManger is in the splash Activity, it executes each time the app is launched. And so if the time for the notification is past, the app sends the notification right away. Two: If the time for the notification hasn’t occured yet, the app crashes because in the MainActivity I have a call to clear the notification, and since the notification hasn’t fired yet, the call to clear the notification crashes the MainActivity. I know it’s the clear notification call that crashes the MainActivity because if I comment the call out, the app runs fine.

Question: Is there a way to code the notification so it doesn’t load each time the app is launched? And could I write the clear the notification bit so that it doesn’t crash the app if it hasn’t fired? Here’s the notification that’s in the Splash Activity:

    private void launchAlarmManager() {
    //---- ALARM MANAGER ------
    //---use the AlarmManager to trigger an alarm---
    AlarmManager aMan = (AlarmManager) getSystemService(ALARM_SERVICE);

    //---get current date and time---
    Calendar alCal = Calendar.getInstance();
    //---sets the time for the alarm to trigger---                       
    alCal.set(Calendar.HOUR_OF_DAY, 12);            
    alCal.set(Calendar.MINUTE, 25);                
    alCal.set(Calendar.SECOND, 00);

    //---PendingIntent to launch activity when the alarm triggers---                    
    Intent iDN = new Intent("com.myapp.DISPLAYNOTIFICATIONS");

    PendingIntent pendA = PendingIntent.getActivity(this, 0, iDN, 0);
    //---sets the alarm to trigger--- 
    aMan.set(AlarmManager.RTC_WAKEUP, alCal.getTimeInMillis(), pendA);

    //---- END ALARM MANAGER ------

and here’s the cancel notification bit in the MainActivity:

NotificationManager nm;
//---cancel the notification by getting the Unique ID from the DisplayNotification class---
nm.cancel(getIntent().getExtras().getInt("uID"));
  • 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-06-02T19:43:27+00:00Added an answer on June 2, 2026 at 7:43 pm

    I guess you need to save the status on whether the alarm has already been set. In pseudocode:

    load the alarm_has_been_set variable
    if( !alarm_has_been_set ) {
        set alarm
        save alarm_has_been_set = true
    }
    

    Then, once the alarm triggers, you unset that variable. For saving and loading see Making data persistent in Android.

    As to your second question about crashing when cancelling the notification, try using a try-catch block:

    try {
        nm.cancel(getIntent().getExtras().getInt("uID"));
    } catch (Exception e) {
        System.out.println("Error when cancelling: "+e.toString());
    }
    

    Also, I just noticed that at least your example code will produce a NullPointerException as you are not initializing the NotificationManager class at all.

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

Sidebar

Related Questions

I have a parent-child relationship setup that is fairly basic. The end result is
I have a fairly basic set of models where a 'Project' has one 'Owner'.
I've written a fairly basic app for the iPhone, which I would like to
I have a rails app that has picked up a bit of traction, but
A client has an iOS app that makes fairly heavy use of UiWebViews (with
I have a fairly basic TCP server keeping track of a couple connections and
I am making a webapp. I have a fairly basic question about javascript performance.
basic c++ question i'm fairly sure. if i have a base class with a
We have fairly large C++ application which is composed of about 60 projects in
I have a fairly small MySQL database (a Textpattern install) on a server that

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.