Reputation: 9894
I would like to add an event to Google calendar from my android app.
Firstly i select google calendar's calendar id:
If calendar's name contains "@gmail.com" that is it. (I coudnt found better method, suggest me if you have any better.)
private long getGoogleCalendarId() {
String[] projection = new String[] { Calendars._ID, Calendars.NAME, Calendars.ACCOUNT_NAME, Calendars.ACCOUNT_TYPE,
};
Cursor calCursor = ctx.getContentResolver()
.query(Calendars.CONTENT_URI, projection, Calendars.VISIBLE + " = 1", null, Calendars._ID + " ASC");
if (calCursor.moveToFirst()) {
do {
long id = calCursor.getLong(0);
String displayName = calCursor.getString(1);
if (displayName.contains("@gmail.com")) {
return id;
}
} while (calCursor.moveToNext());
}
return -1;
}
I save the id and then try to add a sample event:
public void addEventToGoogleCalendar() {
long calId = googleCalendarId;
if (calId == -1) {
return;
}
long start = getNowDate();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, start);
values.put(Events.DTEND, start);
values.put(Events.TITLE, "Some title");
values.put(Events.CALENDAR_ID, calId);
values.put(Events.EVENT_TIMEZONE, "Europe/Berlin");
values.put(Events.DESCRIPTION, "The agenda or some description of the event");
values.put(Events.ALL_DAY, 1);
values.put(Events.ORGANIZER, "[email protected]");
Uri uri = ctx.getContentResolver().insert(Events.CONTENT_URI, values);
long eventId = Long.valueOf(uri.getLastPathSegment());
Log.i("adding event to Cal: ", eventId + "");
}
Logcat shows, that my addings have some id-s like 386, 387, etc, each time it is incremented so i guess SOMETHING is definetly happening, otherwise i would have to get Exception or -1 returning.
If i go to: https://www.google.com/calendar I can't see my new added event.
I just started to study this Google calendar thing so my code could be wrong anywhere. Please assist me in some ways. Is it even possible what iam trying to do?
Upvotes: 1
Views: 1620
Reputation: 214
Adding an event into the Google Calendar only stores it locally until the calendar gets synced. Getting increasing eventIds means that the events are put into the calendar. To check this, install the Google Calendar app from the Play Store on your android device, and first check if the events are there. All the events you added should show up there. If everything goes well, also make sure the "Sync Calendars" option is checked in your Android device settings, and make sure your the device is connected to the internet. As soon as the calendar is synced, your events will also show up in calendar.google.com
Upvotes: 1