Reputation: 1019
private void setEvent(){
long startMilis = 0;
int mCalId = 1;
long endMilis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2013, 1, 29, 9, 10);
startMilis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2013, 1, 30, 10,10);
endMilis = endTime.getTimeInMillis();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.CALENDAR_ID, mCalId);
values.put(Events.DTSTART, startMilis);
values.put(Events.DTEND, endMilis);
values.put(Events.TITLE,"Special Event");
values.put(Events.DESCRIPTION, "Group Activity");
values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
Uri uri = cr.insert(Events.CONTENT_URI, values);
Toast.makeText(this, "Event Added", Toast.LENGTH_LONG).show();
}
This is snippet of my code where i want to add Event to the android calendar. Code works fine.
But when tested on device, The specified event does not actually get added and does not appear in calendar.Code is totally Error free and i have provided necessary permissions.
Can someone please tell me where exactly i am doing wrong.
Upvotes: 4
Views: 2147
Reputation: 2927
Maybe you are usin wrong Uri, i use this in my app:
Uri EVENTS_URI = Uri.parse(CalendarContract.Events.CONTENT_URI.toString());
ContentResolver cr = getActivity().getContentResolver();
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put(Events.TITLE, recordatorio);
values.put(Events.ALL_DAY, 1);
values.put(Events.EVENT_LOCATION, lugar);
values.put("dtstart", calDate.getTimeInMillis());
values.put("dtend", calDate.getTimeInMillis());
values.put(Events.DESCRIPTION, observaciones);
values.put("availability", 0);
values.put(Events.HAS_ALARM, true);
values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().toString());
Uri uri = cr.insert(EVENTS_URI, values);
// to get the Id Event
long eventID = Long.parseLong(uri.getLastPathSegment());
Upvotes: 0
Reputation: 55340
If there are no errors, but nevertheless the event does not appear in the Calendar,
I would suspect that the culprit is the mCalId = 1
assignment.
A device can have multiple calendars. It's not guaranteed that the one with id == 1 is the primary one (and even if you used the primary one, the user may have events in multiple different calendars -- e.g. personal and work).
So, it depends on what you want to do exactly. You should either:
IS_PRIMARY
, but see caveat above), orcalendar_id
from then on.Upvotes: 3