0

I'm trying to make a notification to go off on Thursdays and Fridays by using the following code: This code is for thursdays

Calendar updateTime = Calendar.getInstance();
updateTime.set(Calendar.HOUR_OF_DAY, 14);
updateTime.set(Calendar.MINUTE, 44);
updateTime.set(Calendar.SECOND, 0);
updateTime.set(Calendar.DAY_OF_WEEK, 5);
if(updateTime.before(new GregorianCalendar())){
    updateTime.add(GregorianCalendar.DAY_OF_MONTH, 7);
}
Intent intent = new Intent(context, Alarm4.class);
intent.putExtra(ONE_TIME, Boolean.TRUE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), pi);

However, this code does not work the notification pops-up directly if the time did already past the notification time.

What am I missing?

user1839995
  • 113
  • 2
  • 9

2 Answers2

1

Do like this.

           if (updateTime.getTimeInMillis() < System.currentTimeMillis()

                || updateTime.before(new GregorianCalendar())) {
            updateTime.set(Calendar.HOUR_OF_DAY, hourOfDay + 24);
            updateTime.add(GregorianCalendar.DAY_OF_MONTH, 7);
        }

and make a class to set Alarm.

        public class setAlarm {

Context context;

public setAlarm(Context c) {
    // TODO Auto-generated constructor stub
    this.context = c;
}

public void settingAlarm(Calendar calendar, int counter) {
    Intent intent = new Intent(context, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
            counter, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
            pendingIntent);
}

}

and Alarm BroadCastReceiver is public class AlarmReceiver extends BroadcastReceiver {

private NotificationManager manager;
private int NOTIFICATION_ID;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "Alarm Received", Toast.LENGTH_LONG).show();
    manager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher,
            "Food Time", System.currentTimeMillis());
    Intent intent2 = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent2, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, "FoodTime",
            "Click to View your food", pendingIntent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notification.sound = Uri
            .parse("android.resource://"
                    + "com.technogalax.sixsmallmealaday.alarm" + "/"
                    + R.raw.nsound);

    manager.notify(NOTIFICATION_ID, notification);

}

}

Rayyan
  • 186
  • 2
  • 11
0

Apparently, your code is ok, but half-done. Now, you have to create a BroadcastReceiver, from where you have to set the notification. You can go through this article, this article will be helpful for you. I like to mention here that it will set single alarm for user, not repeating. For repeating, you have to use public void setRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation) instead of public void set (int type, long triggerAtMillis, PendingIntent operation). Still it will repeat on a regular interval.

In my idea for your specific type of repeation, you can arrange it in different way. Initially set the alarm just for next day(after checking next day for alarm) using public void set (int type, long triggerAtMillis, PendingIntent operation). It will be very much simple. When the Alarm will be broadcasted by the system and your BroadcastReceiver will be called. In onRecieve() your BroadcastReceiver, set the Notification and set the alarm for next day(after checking next day for alarm). In this way the total recurring process will be in your control and you can easily set any logic for it.

dev_android
  • 8,698
  • 22
  • 91
  • 148
  • Isn't there a way to do it the way I do above, I'm a beginner with Android Development so I'm not 100% sure what you mean by this. – user1839995 Jan 04 '13 at 12:51