10

I have an android app that downloads with a service some drugs info.

For example (fludex white round 2 24-02-2012),means a drug named fludex ,white and round,must be given 2 times per day from today untill 24-01-2012.

Now i want after drug info downloading , to add repeated event with drug info to the calendar silently/programmatically(without user interaction). So that from today untill 24-01-2012 every 10 am and 10pm to have a reminder 10 minutes before to take his drug. My app will be for android 2-4. How can i do that,i'm confused from my searching so far.

Second question:How can i delete only the events(and their reminders) made from my application,so when i sync my drug therapy to delete all previous events and produce new events based on the new drug therapy i receive from my service?

oikonomopo
  • 4,025
  • 7
  • 44
  • 73
  • have a look at this issue: http://stackoverflow.com/questions/28871921/add-weekly-event-to-calendar – Sun Jul 01 '15 at 12:39

3 Answers3

19
        ContentResolver cr = ctx.getContentResolver();
        ContentValues values = new ContentValues();
            
        values.put(CalendarContract.Events.DTSTART, dtstart);
        values.put(CalendarContract.Events.TITLE, title);
        values.put(CalendarContract.Events.DESCRIPTION, comment);
        
        TimeZone timeZone = TimeZone.getDefault();
        values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());

        // default calendar
        values.put(CalendarContract.Events.CALENDAR_ID, 1);

        values.put(CalendarContract.Events.RRULE, "FREQ=DAILY;UNTIL="
                + dtUntill);
        // for one hour
        values.put(CalendarContract.Events.DURATION, "+P1H");

        values.put(CalendarContract.Events.HAS_ALARM, 1);

        // insert event to calendar
        Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

where dtuntil is

    SimpleDateFormat yyyymmdd = new SimpleDateFormat("yyyymmdd");
    Calendar dt = Calendar.getInstance();

    // where untilDate is a date instance of your choice,for example 30/01/2012
    dt.setTime(untilDate);

    // if you want the event until 30/01/2012 we add one day from our day
    // because UNTIL in RRule sets events Before the last day want for event
    dt.add(Calendar.DATE, 1);
    String dtUntill = yyyymmdd.format(dt.getTime());

    // Uri
    Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

    // get the event ID that is the last element in the Uri
    long eventID = Long.parseLong(uri.getLastPathSegment());

    // add 10 minute reminder for the event
    ContentValues reminders = new ContentValues();
    reminders.put(Reminders.EVENT_ID, eventID);
    reminders.put(Reminders.METHOD, Reminders.METHOD_ALERT);
    reminders.put(Reminders.MINUTES, 10);

    Uri uri = cr.insert(Reminders.CONTENT_URI, reminders);

Ref: Recurrence Rule

Community
  • 1
  • 1
oikonomopo
  • 4,025
  • 7
  • 44
  • 73
  • ctx is a Context variable. – oikonomopo Jun 04 '14 at 18:05
  • @oikonomopo I get a 09-23 00:00:12.632: E/AndroidRuntime(813): android.database.sqlite.SQLiteException: expected 1 row from this query but query returned no data. check the query: SELECT canPartiallyUpdate FROM view_events WHERE _id = ? error – Ruch1234 Sep 22 '14 at 18:30
  • This is perfectly working fine on my HTC device 4.0.2 but same code not working in samsung device 4.4.2 .. Event rrule is :- values.put(CalendarContract.Events.RRULE,"FREQ=WEEKLY;COUNT=16;BYDAY=TU); This event show only for few weeks. Any guess why? – Deepak Sharma May 07 '15 at 13:02
2

Here is a good Example of what you want.

Update for more information about calender and implementing reminders or other stuff see this

You can also get help from the following code

Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", date);
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY"); //To set the repeat rule
intent.putExtra("endTime", date);
intent.putExtra("title", summary);
Ali Imran
  • 8,927
  • 3
  • 39
  • 50
  • This creates/set a reminder/alarm on a certain date.But i want to make this repeat for N days from now.And i also i want insert events to the calendar,so that the user can see them in. – oikonomopo Nov 30 '12 at 20:09
  • can we put duration using putExtra i.e. intent.putExtra("duration", custom time); – Ali Mar 17 '15 at 07:54
2

To create a time bound reoccuring event over multiple days, we need to use CalendarContract.Events.RRULE. The rule is combination frequency, count etc.

Lets say we need to create an event occuring daily for 10 days in a specific time period of day:

Intent(Intent.ACTION_INSERT)
            .setData(CalendarContract.Events.CONTENT_URI)
            .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
                    beginCalendarTime.getTimeInMillis())
            .putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
                    endCalendarTime.getTimeInMillis())
            .putExtra(CalendarContract.Events.TITLE, heading)
            .putExtra(CalendarContract.Events.DESCRIPTION, "To be added")
            .putExtra(CalendarContract.Events.EVENT_LOCATION, location)
            .putExtra(CalendarContract.Events.RRULE, "FREQ=DAILY;COUNT=10");
saurabh dhillon
  • 798
  • 7
  • 17