11

i'm writing an application that need to add some events to a calendar in android. For inserting i just used the following code:

public void onItemClick(AdapterView<?> adapter, View curview, int position, long id) {
    WhoisEntry entry = this.adapter.getItem(position);      
    String domainName = entry.getDomainName();
    Date expDate = entry.expirationDate;
    Toast.makeText(getApplicationContext(), "Domain: " + domainName, Toast.LENGTH_SHORT).show();
    Calendar cal = Calendar.getInstance();            
    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("beginTime", entry.expirationDate);
    intent.putExtra("allDay", false);       
    intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
    intent.putExtra("title", "Expiration of " + entry.domainName);
    startActivity(intent);
}

Now i'm wondering if is possible to get an id associated to that event, in that way after an event is inserted, and its id is saved into my application, the user can recall that event directly from inside the application. Is it possible?

Ivan
  • 4,186
  • 5
  • 39
  • 72

2 Answers2

12

I extracted a list of columns used to store events into android calendar. Here the list:

[0] "originalEvent" (id=830007842672)
[1] "availabilityStatus" (id=830007842752)
[2] "ownerAccount" (id=830007842840)
[3] "_sync_account_type" (id=830007842920)
[4] "visibility" (id=830007843008)
[5] "rrule" (id=830007843080)
[6] "lastDate" (id=830007843144)
[7] "hasAlarm" (id=830007843216)
[8] "guestsCanModify" (id=830007843288) [9] "guestsCanSeeGuests" (id=830007843376)
[10] "exrule" (id=830007843464)
[11] "rdate" (id=830007843528)
[12] "transparency" (id=830007843592)
[13] "timezone" (id=830007843672)
[14] "selected" (id=830007843744)
[15] "dtstart" (id=830007843816) [16] "title" (id=830007843888)
[17] "_sync_time" (id=830007843952)
[18] "_id" (id=830007844024) [19] "hasAttendeeData" (id=830007844088) [20] "_sync_id" (id=830007844176)
[21] "commentsUri" (id=830007844248) [22] "description" (id=830007844328) [23] "htmlUri" (id=830007844408) [24] "_sync_account" (id=830007844480)
[25] "_sync_version" (id=830007844560)
[26] "hasExtendedProperties" (id=830007844640)
[27] "calendar_id" (id=830007844736)

Then if i want to get the new event id for my event:

public static long getNewEventId(ContentResolver cr, Uri cal_uri){      
    Uri local_uri = cal_uri;
    if(cal_uri == null){
        local_uri = Uri.parse(calendar_uri+"events");
    } 
    Cursor cursor = cr.query(local_uri, new String [] {"MAX(_id) as max_id"}, null, null, "_id");
    cursor.moveToFirst();
    long max_val = cursor.getLong(cursor.getColumnIndex("max_id"));     
    return max_val+1;
}

ANd for insert event:

public void insertDomainEntry(Date exp_date, String name, long event_id){
    SQLiteDatabase db = getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("exp_date", exp_date.getTime()/1000);
    values.put("event_id", event_id);
    values.put("domainname", name);
    db.insertOrThrow("domains_events", null, values);
}

That solution seems to work, even if probably this is not a very good solution.

EDIT 02/2015 The purpose of getNextEventId is to create a new Event Entry for the event table, here the code with the usage of this method:

@Override
    public void onItemClick(AdapterView<?> adapter, View curview, int position,
            long id) {
        WhoisEntry entry = this.adapter.getItem(position);      
        long event_id = CalendarUtils.getNewEventId(getContentResolver(), null);

        Toast.makeText(getApplicationContext(), "Domain: " + entry.getDomainName(),
                Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("beginTime", entry.getExpiration().getTime());
        intent.putExtra("_id", event_id);
        intent.putExtra("allDay", false);       
        intent.putExtra("endTime", entry.getExpiration().getTime()+60*30);
        intent.putExtra("title", "Expiration of " + entry.getDomainName());
        startActivity(intent);

        database.insertDomainEntry(entry.getExpiration(),
                entry.getDomainName(), event_id);
    }

Update 09/2015

As requested in the comment i add how to get the Calendar URI (it is basically where the calendar is stored, and the application try to guess it, searching in all known possible calendar paths)

public static String getCalendarUriBase(Activity act) {     
    String calendarUriBase = null;
    Uri calendars = Uri.parse("content://calendar/calendars");
    Cursor managedCursor = null;

    try {
        managedCursor = act.getContentResolver().query(calendars,
                null, null, null, null);
    } catch (Exception e) {
    }

    if (managedCursor != null) {
        calendarUriBase = "content://calendar/";
    } else {
        calendars = Uri.parse("content://com.android.calendar/calendars");
        try {
            managedCursor = act.getContentResolver().query(calendars,
                    null, null, null, null);
        } catch (Exception e) {
        }
        if (managedCursor != null) {
            calendarUriBase = "content://com.android.calendar/";
        }
    }

    calendar_uri= calendarUriBase;
    return calendarUriBase;
}
Ivan
  • 4,186
  • 5
  • 39
  • 72
  • how do you know that the last event is what your user added? maybe it is an older event? (the user canceled) – dowi Feb 19 '15 at 13:25
  • Why i need to know about the last event? When i insert a new event i just need to know about the id Calendar entry that i just created, and when i create an element, i also have its id. And if you need to update them in another moment, probably is better to save references to the calendar/event item in the application (using sqlite or something). – Ivan Feb 19 '15 at 13:38
  • you are getting the MAX(id) so i assume you get the last event that was adde to the calendar. but how do you know that the user did not canceled the calendar activity that you opened with startActivity(intent) ? – dowi Feb 19 '15 at 13:45
  • Ok it was a while that i didn't worked with this stuff, and i nearly forgot what i did. I'll update my answer with the other piece of code that explain the meaning of getNextEventId, but briefly i use getNextEventId to create the event entry in the events table, but no the real calendar entry, it will be created by the Calendar Intent, i don't remember why i did this :D – Ivan Feb 19 '15 at 13:58
  • Be careful, using getNewEventId(max_val+1).. consider if user deleted last event .. adding new event with used ID may lead to un-expected result if there are x-records related to that ID i.e. remainder or attendee. – Maher Abuthraa Jun 07 '16 at 10:59
  • @SagarNayak unfortuately not, maybe you can check if they changed again the calendar uri? maybe is under a different URI? – Ivan Feb 19 '20 at 14:15
0

You can easily get event id after inserting an event.

long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();
...

ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Jazzercise");
values.put(Events.DESCRIPTION, "Group workout");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
Uri uri = cr.insert(Events.CONTENT_URI, values);

// get the event ID that is the last element in the Uri
long eventID = Long.parseLong(uri.getLastPathSegment());
//
// ... do something with event ID
//
//
Gary Chen
  • 248
  • 2
  • 14