Click here to Skip to main content
15,905,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
Java
// Storing selected date
                Date dt = null;
                try { dt = dateFormatter.parse(dateSelected); } // dateSelected - String for storing the date user chose
                catch (final java.text.ParseException e) { e.printStackTrace(); }

                final Calendar BDay = Calendar.getInstance(); // Setting calendar for the next birthday
                BDay.setTime(dt);
                final Calendar today = Calendar.getInstance(); // Setting calendar for the current date

                // Take your DOB Month and compare it to current month
                final int BMonth = BDay.get(Calendar.MONTH);
                final int CMonth = today.get(Calendar.MONTH);
                BDay.set(Calendar.YEAR, today.get(Calendar.YEAR));
                // Result of next birthday
                if(BMonth <= CMonth)
                {
                    BDay.set(Calendar.YEAR, today.get(Calendar.YEAR) + 1);
                }
                // Result in millis
                final long millis = BDay.getTimeInMillis() - today.getTimeInMillis();
                // End time
                final long end = BDay.getTimeInMillis();
                // Convert to days
                final long days = millis / 86400000; // Precalculated (24 * 60 * 60 * 1000)

                // Setting Alarm Manager
                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:

Java
<pre>// addReminder - button
        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:

Java
public class AlarmReceiver extends BroadcastReceiver{

    public int MID;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        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.
Posted
Comments
Member 12084016 21-Nov-15 6:43am    
Note: In RecurrencePickerDialog user is chosing how many time he wants to get notification until that specifig upcoming birthday. And i'm storing the result in one String.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900