Вопрос

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"));
Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top