Creating a calendar for specific dates in Android?

I have a special requirement for creating a calendar event that will occur on specific dates. This is not a scenario that will occur Weekly, monthly, annually, but may occur 13 days, 14 days, 15 days, etc. I have what dates this will happen, but have not set recurring dates. below is my code

ContentValues calEvent = new ContentValues();
            calEvent.put(CalendarContract.Events.CALENDAR_ID, 1);
            calEvent.put(CalendarContract.Events.TITLE, title);
            calEvent.put(CalendarContract.Events.DTSTART, d.getTime());
            calEvent.put(CalendarContract.Events.DTEND, d.getTime()
                    + (2 * 60 * 60 * 1000));
            calEvent.put(CalendarContract.Events.HAS_ALARM, 1);
            calEvent.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone
                    .getDefault().getID());
            calEvent.put(CalendarContract.Events.EVENT_LOCATION, location);
            calEvent.put(CalendarContract.Events.DESCRIPTION, description);
            calEvent.put(CalendarContract.Events.RDATE, sdf1.format(d)
                    + "T033000Z");
            // calEvent.put(CalendarContract.Reminders.RRULE, value);
            Uri uri = contentResolver.insert(
                    CalendarContract.Events.CONTENT_URI, calEvent);

            // The returned Uri contains the content-retriever URI for the
            // newly-inserted event, including its id
            int id = Integer.parseInt(uri.getLastPathSegment());
            // Toast.makeText(ctx, "Created Calendar Event " + id,
            // Toast.LENGTH_SHORT).show();
            eventId = eventId + (eventId.equalsIgnoreCase("") ? id : "," + id);
            ContentValues calReminder = new ContentValues();

            calReminder.put(CalendarContract.Reminders.MINUTES, minutes);
            calReminder.put(CalendarContract.Reminders.EVENT_ID, id);
            // calReminder.put(CalendarContract.Reminders.HAS_ALARM, 1);
            calReminder.put(Reminders.METHOD, Reminders.METHOD_ALERT);
            Uri uri1 = contentResolver.insert(
                    CalendarContract.Reminders.CONTENT_URI, calReminder);

            // The returned Uri contains the content-retriever URI for the
            // newly-inserted event, including its id
            int id1 = Integer.parseInt(uri1.getLastPathSegment());
            // Toast.makeText(ctx, "Created Calendar Reminder " + id1,
            // Toast.LENGTH_SHORT).show();
            reminderId = reminderId
                    + (reminderId.equalsIgnoreCase("") ? id1 : "," + id1);

When I use RDATE with comma-separated dates, provided there are startdate and enddate, an Event is generated for all dates between startdate and enddate.

+4
source share
1 answer
Intent service_intent = new Intent(your application context,
                            your class.class);

                    PendingIntent pintent = PendingIntent.getService(
                            your application context, 0, service_intent, 0);

                    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                    // for 30 mint 60*60*1000
                    alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                            cal.getTimeInMillis(), APP_DAY_FOR_SERVICE
                                    * APP_HOUR_FOR_SERVICE
                                    * APP_MINUT_FOR_SERVICE
                                    * APP_SECOND_FOR_SERVICE * 1000,
                            pintent);

24 , APP_DAY_FOR_SERVICE 2, 48 , , , , , APP_DAY_FOR_SERVICE,

APP_DAY_FOR_SERVICE APP_HOUR_FOR_SERVICE APP_MINUT_FOR_SERVICE, , - ,

+1

Source: https://habr.com/ru/post/1545617/


All Articles