Search code examples
androidandroid-alarms

Setting repeating alarm when creating Application - why does it trigger immediately


I have an application object declared in the manifest and this code runs when the application runs. What I want to accomplish is to set an alarm:

@Override
public void onCreate() {

    super.onCreate();
    singleton = this;
    persister = new Persister();
    am = (AlarmManager) getSystemService(ALARM_SERVICE);
    sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    sharedPref.registerOnSharedPreferenceChangeListener(this);
    registerAlarms();
}

public void registerAlarms() {

    String sleepString = sharedPref.getString("time_sleepLog", "08:00");
    String[] pieces = sleepString.split(":");
    int sleepHour = Integer.parseInt(pieces[0]);
    int sleepMinute = Integer.parseInt(pieces[1]);

    String eveningString = sharedPref.getString("time_eveningLog", "20:00");
    pieces = null;
    pieces = eveningString.split(":");
    int eveningHour = Integer.parseInt(pieces[0]);
    Log.v(TAG, "eveningHour in registerAlarms: " + eveningHour);
    int eveningMinute = Integer.parseInt(pieces[1]);
    // create calendar objects pointing to the next time this clock will
    // occur
    Calendar sleepCal = Calendar.getInstance();
    sleepCal.set(Calendar.HOUR_OF_DAY, sleepHour);
    sleepCal.set(Calendar.MINUTE, sleepMinute);
    sleepCal.set(Calendar.SECOND, 0);

    Calendar eveningCal = Calendar.getInstance();
    eveningCal.set(Calendar.HOUR_OF_DAY, eveningHour);
    eveningCal.set(Calendar.MINUTE, eveningMinute);
    sleepCal.set(Calendar.SECOND, 0);
    Intent syncIntent = new Intent(this, SleepNotificationReceiver.class);
    syncIntent.putExtra("MoodSleepLogAlarm", 0);
    PendingIntent sleepPending = PendingIntent.getBroadcast(this, 0,
            syncIntent, 0);
    // then set the alarms
    am.setRepeating(AlarmManager.RTC_WAKEUP, sleepCal.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, sleepPending);
    Log.v(TAG, "Alarm for sleep registered at " + sleepCal.getTime());
    }

I log in the broadcastreceiver to check if it runs with:

Log.v(TAG, "Context: " + context.getClass().getName())

I can see this in logcat 4 seconds after starting my application as 0

6-27 17:59:29.492: V/SleepNotificationReceiver(2609): Context: android.app.ReceiverRestrictedContext

When I call the registerAlarms() via a button it doesn't happend. So it is only when called onCreate.

Why does it run the broadcast receiver after 4 seconds? (It does also run the broadcastreceiver with the same context given at the given times in my settings screen - but I need it to not run when I set it.)


Solution

  • When you are setting the alarm repeating, the second parameter indicates when the alarm has to go off the first time, so I think that if you type sleepCal.getTimeInMillis() and you are in the "future" compared to this time, the alarm is going off instantly.

    Check that second parameter if it's in the future because I think that you are not setting the day or month in your calendar.