I try to create an event and add an email to it.
i cant get it work, the time i get is 1970 and no email apears.
any ideas?
Thanks.
This is the code i use:
Uri calendars = null;
if (Integer.parseInt(Build.VERSION.SDK) == 8 )
{
calendars = Uri.parse("content://com.android.calendar/calendars");
}
else
{
calendars = Uri.parse("content://calendar/calendars");
}
Cursor cursor = context.getContentResolver().query(calendars, new String[] { "_id", "displayName" }, "selected=1", null, null);
if (cursor != null && cursor.moveToFirst())
{
String[] calNames = new String[cursor.getCount()];
final int[] calIds = new int[cursor.getCount()];
for (int i = 0; i < calNames.length; i++)
{
// retrieve the calendar names and ids
calIds[i] = cursor.getInt(0);
calNames[i] = cursor.getString(1);
cursor.moveToNext();
}
cursor.close();
String name = "Name1";
String eMail = "name1@nnn.com";
ContentValues event = new ContentValues();
int cal_id = calIds[which];
event.put("calendar_id", cal_id);
event.put("title", "test title");
event.put("eventLocation", "test location");
event.put("eventStatus", 1);
event.put("visibility", 0);
event.put("transparency", 0);
event.put("hasAlarm", 1);
event.put("hasAttendeeData", "1");
Calendar c = Calendar.getInstance();
c.roll(Calendar.DAY_OF_MONTH, true);
Date date = c.getTime();
Date begine = new Date(date.getYear(),
date.getMonth(),
date.getDate(),
14, 0); long dtstart = begine.getTime();
event.put("dtstart", dtstart);
Date end = new Date(date.getYear(),
date.getMonth(),
date.getDate(),
15, 0);
long dtend = end.getTime();
event.put("dtend", dtend);
Uri eventsUri = null;
if (Integer.parseInt(Build.VERSION.SDK) == 8 )
{
eventsUri = Uri.parse("content://com.android.calendar/events");
}
else
{
eventsUri = Uri.parse("content://calendar/events");
}
Uri url = context.getContentResolver().insert(eventsUri, event);
long id = -1;
if (url != null)
{
id = Long.parseLong(url.getLastPathSegment());
ContentValues values = new ContentValues();
values.put("event_id", id);
values.put("method", 1); //METHOD_ALERT
values.put("minutes", 15); // 15 minutes
Uri reminder = Uri.parse("content://com.android.calendar/reminders");
context.getContentResolver().insert(reminder, values);
if(name.length() > 0 || eMail.length() > 0)
{
ContentValues attendees = new ContentValues();
attendees.put("attendeeEmail", eMail);
attendees.put("attendeeName", name);
attendees.put("attendeeRelationship", 2);//RELATIONSHIP_ATTENDEE
attendees.put("attendeeStatus", 3); //ATTENDEE_STATUS_INVITED
attendees.put("attendeeType", 1); //TYPE_REQUIRED
attendees.put("event_id", id);
Uri attendeesUri = null;
if (Integer.parseInt(Build.VERSION.SDK) == 8 )
{
attendeesUri = Uri.parse("content://com.android.calendar/attendees");
}
else
{
attendeesUri = Uri.parse("content://calendar/attendees");
}
context.getContentResolver().insert(attendeesUri, attendees);
Intent i = new Intent(Intent.ACTION_EDIT);
i.setType("vnd.android.cursor.item/event");
i.setData(url);
context.startActivity(i);
}
else
{
Toast.makeText(context, "Could not create an event!", Toast.LENGTH_LONG);
}
The time for dtStart and dtEnd should be in milliseconds after the Epoch (Jan. 1, 1970) use this link epoch time converter to get the current time. Multiply the time it gives you on that site by 1000 because it returns it in seconds instead of milliseconds.
Also, dtEnd and dtStart is of the “long” type.