I'm using
this library for getting recurrence picker dialog. I have also one DatePickerDialog where user is setting his date of birthday. After that i'm calculating when will be his next birthday from now. Now i have to set AlarmManager so i could get notification for the next birthday. Here is the code how i'm getting the date of birthday and how i'm calculating his next birthday:
Date dt = null;
try { dt = dateFormatter.parse(dateSelected); }
catch (final java.text.ParseException e) { e.printStackTrace(); }
final Calendar BDay = Calendar.getInstance();
BDay.setTime(dt);
final Calendar today = Calendar.getInstance();
final int BMonth = BDay.get(Calendar.MONTH);
final int CMonth = today.get(Calendar.MONTH);
BDay.set(Calendar.YEAR, today.get(Calendar.YEAR));
if(BMonth <= CMonth)
{
BDay.set(Calendar.YEAR, today.get(Calendar.YEAR) + 1);
}
final long millis = BDay.getTimeInMillis() - today.getTimeInMillis();
final long end = BDay.getTimeInMillis();
final long days = millis / 86400000;
Intent intent = new Intent(AddBirthday.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(AddBirthday.this,
0 , intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) AddBirthday.this.getSystemService(AddBirthday.this.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, end, AlarmManager.INTERVAL_DAY, pendingIntent);
<pre lang="text">
Now my question is what is the easiest way to use this with recurrence picker. And the above code is set in one simple method, but because String dateSelected is static i could use it also in recurrence picker with same calculation. Here is the code for recurrence picker:
<pre>
if (v == addReminder) {
RecurrencePickerDialog recurrencePickerDialog = new RecurrencePickerDialog();
if (recurrenceRule != null && recurrenceRule.length() > 0) {
Bundle bundle = new Bundle();
bundle.putString(RecurrencePickerDialog.BUNDLE_RRULE, recurrenceRule);
recurrencePickerDialog.setArguments(bundle);
}
recurrencePickerDialog.setOnRecurrenceSetListener(new RecurrencePickerDialog.OnRecurrenceSetListener() {
@Override
public void onRecurrenceSet(String rrule) {
recurrenceRule = rrule;
if (recurrenceRule != null && recurrenceRule.length() > 0) {
EventRecurrence recurrenceEvent = new EventRecurrence();
recurrenceEvent.setStartDate(new Time("" + new Date().getTime()));
recurrenceEvent.parse(rrule);
String srt = EventRecurrenceFormatter.getRepeatString(AddBirthday.this, getResources(), recurrenceEvent, true);
addReminder.setText(srt);
} else {
addReminder.setText("No recurrence");
}
}
});
recurrencePickerDialog.show(getSupportFragmentManager(), "recurrencePicker");
}
And my AlarmReciever class:
public class AlarmReceiver extends BroadcastReceiver{
public int MID;
@Override
public void onReceive(Context context, Intent intent) {
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
context).setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Alarm Fired")
.setContentText("It's " + "" + "birthday").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
}
}
I'm really stuck here. This is the last thing left in my app to be finished, so guys i would appreciate any help here. Thank you.