0

I followed the instructions in this thread to create a daily alarm that starts a service at 12:30 each day which shows a notification. The problem I'm having is that setting the alarm also triggers the service (=> and the notification) every time the app starts.

Since I figured that the alarm will run only at the specified time (12:30) then I have no problem setting it when the app starts.

I realize that setting the alarm from scratch every time the app is launched is a bit ineffective since it only needs to be set once (I made it set on device boot as well), but it seemed like the easiest way.

So what's the best way to fix this? is there a way to set the alarm for the specified time without running the intent when setting?

Here's the code if you are interested (this function is called every time when launching the app):

public static void setAlarm(Context context)
{
    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.setAction("com.Rani.app.SET_NOTIFICATION_ALARM");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar dailyCheckTime = Calendar.getInstance();
    dailyCheckTime.setTimeZone(TimeZone.getTimeZone("GMT"));
    dailyCheckTime.set(Calendar.HOUR_OF_DAY, 12);
    dailyCheckTime.set(Calendar.MINUTE, 30);
    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarm.cancel(pendingIntent);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, dailyCheckTime.getTimeInMillis(), 
            AlarmManager.INTERVAL_DAY, pendingIntent);
}

thanks in advance.

Community
  • 1
  • 1
Rani
  • 47
  • 5
  • Random guess, have you tried setting the date to tomorrow? `dailyCheckTime.set(Calendar.DAY_OF_MONTH, tomorrow);` tomorrow would be a calculated var. pseudo : calendar.getDayOfMonth()+1 – Blundell Mar 11 '12 at 22:58
  • I tried, but it still ran it. I posted a solution below – Rani Mar 12 '12 at 23:52

1 Answers1

0

After trying several things, I used the information in this thread to preform a check if an alarm already exists, and setting one only if there isn't one already.

code:

public static void setAlarm(Context context)
{
    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.setAction("com.Rani.app.SET_NOTIFICATION_ALARM");

    boolean alarmUp = (PendingIntent.getBroadcast(context,
            0, intent, PendingIntent.FLAG_NO_CREATE) != null);

    // check if an alarm already exists
    if (alarmUp == false)
    {
        // set an alarm in case there isnt one already set
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar dailyCheckTime = Calendar.getInstance();
        dailyCheckTime.set(Calendar.HOUR_OF_DAY, 12);
        dailyCheckTime.set(Calendar.MINUTE, 30);
        if (dailyCheckTime.getTimeInMillis() < Calendar.getInstance()
                .getTimeInMillis()) {
            dailyCheckTime.set(Calendar.DATE,
                    dailyCheckTime.get(Calendar.DATE) + 1);
        }

        AlarmManager alarm = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(pendingIntent);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                dailyCheckTime.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);
    }
}
Community
  • 1
  • 1
Rani
  • 47
  • 5