I am following this gist, to insert event into Calendar
How do I update existing Calendar Event, which i have inserted earlier using below code:
public void addToCalender() throws ParseException {
......
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.CALENDAR_ID, calendarId[0]);
event.put(CalendarContract.Events.TITLE, "Event Title");
event.put(CalendarContract.Events.DESCRIPTION, "Event Description");
event.put(CalendarContract.Events.EVENT_LOCATION, "Eevnt Location");
event.put(CalendarContract.Events.DTSTART, startCalTime);
event.put(CalendarContract.Events.DTEND, endCalTime);
event.put(CalendarContract.Events.STATUS, 1);
event.put(CalendarContract.Events.HAS_ALARM, 1);
event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
Uri insertEventUri = AddEventActivity.this.getContentResolver().insert(
eventsUri, event);
ContentValues reminders = new ContentValues();
reminders.put(Reminders.EVENT_ID,
Long.parseLong(insertEventUri.getLastPathSegment()));
reminders.put(Reminders.METHOD, Reminders.METHOD_ALERT);
reminders.put(Reminders.MINUTES, 10);
AddEventActivity.this.getContentResolver().insert(remainderUri, reminders);
}
I would like to know, How do I :
1. Remove existing event
2. Update existing event
Here is how you can modify an event. Let's say its ID is eventID:
public void updateEvent(int eventID)
{
ContentResolver cr = context.getContentResolver();
Uri eventUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.TITLE, "new title");
event.put(CalendarContract.Events.DESCRIPTION, "My cool event!");
cr.update(eventUri, event, null, null);
}
To remove an event, you have two possibilities:
If you are a standard application:
Use the previous code and replace all the event.put by this one:
event.put(CalendarContract.Events.DELETED, 1);
If you are the SyncAdapter of the calendar and want a real deletion (The previous method just says that the event should be deleted and the one that follows must be used once the calendar is being synced :
public deleteEvent(Uri eventUri)
{
cr.delete(event, null, null);
}
Where eventUri is the Uri of the event obtained as shown previously from the event ID.
For all of the previous methods, if you get an exception about not being in a sync adapter, you can use this:
public static Uri asSyncAdapter(Uri uri, String account, String accountType)
{
return uri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, account)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, accountType)
.build();
}
I hope this will help you.
Source: personnal code + http://developer.android.com/guide/topics/providers/calendar-provider.html
...
Outlook in fact seems to respond to the rules defined in RFC 2446
In summary you have to specify
METHOD:REQUEST and ORGANIZER:xxxxxxxx
...
Please read the original answer by Tom Carter.
Related
I'm creating a calendar programatically, and everything is setted well but the calendar's name. This is the method I'm using.
private Uri createCalendar(String email){
Uri target = Uri.parse(CalendarContract.Calendars.CONTENT_URI.toString());
target = target.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, email)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google").build();
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars.ACCOUNT_NAME, email);
values.put(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google");
values.put(CalendarContract.Calendars.NAME, context.getResources().getString(R.string.app_name));
values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, context.getResources().getString(R.string.app_name));
values.put(CalendarContract.Calendars.CALENDAR_COLOR, Util.getColor(context, R.color.primary));
values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_ROOT);
values.put(CalendarContract.Calendars.OWNER_ACCOUNT, email);
values.put(CalendarContract.Calendars.VISIBLE, 1);
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
values.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, TimeZone.getDefault().getID());
values.put(CalendarContract.Calendars.CAN_PARTIALLY_UPDATE, 1);
values.put(CalendarContract.Calendars.CAL_SYNC1, "https://www.google.com/calendar/feeds/" + email + "/private/full");
values.put(CalendarContract.Calendars.CAL_SYNC2, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + email);
values.put(CalendarContract.Calendars.CAL_SYNC3, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + email);
values.put(CalendarContract.Calendars.CAL_SYNC4, 1);
values.put(CalendarContract.Calendars.CAL_SYNC5, 0);
values.put(CalendarContract.Calendars.CAL_SYNC8, System.currentTimeMillis());
Uri newCalendar = context.getContentResolver().insert(target, values);
return newCalendar;
}
As said, everything is working fine, the color, the owner account, etc. but when the calendar is created, the name of it is always "Events". I tried to put the name manually (not using getStrings()) but didn't work either.
Any ideas of what can be happening?
I'm trying to create calendar by using code from this topic
private static void createCalendar( Context mContext, Account account)
{
final ContentValues v = new ContentValues();
v.put(CalendarContract.Calendars.NAME,"TEST");
v.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "TEST");
v.put(CalendarContract.Calendars.ACCOUNT_NAME, account.name);
v.put(CalendarContract.Calendars.ACCOUNT_TYPE, account.type);
v.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.GREEN);
v.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
v.put(CalendarContract.Calendars.OWNER_ACCOUNT, account.name);
// v.put(CalendarContract.Calendars._ID, 123);
v.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
v.put(CalendarContract.Calendars.VISIBLE, 1);
Uri creationUri = asSyncAdapter(CalendarContract.Calendars.CONTENT_URI, account.name, account.type);
Uri calendarData = mContext.getContentResolver().insert(creationUri, v);
long id = Long.parseLong(calendarData.getLastPathSegment());
}
private static Uri asSyncAdapter(Uri uri, String account, String accountType)
{
return uri.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true").appendQueryParameter
(CalendarContract.Calendars.ACCOUNT_NAME,account)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, accountType) .build();
}
and then make a call:
createCalendar(c, AccountManager.get(c).getAccountsByType("com.google")[0]); // i've checked - it's correct account to add calendar
Then i retrive data from ContentResolver
final ContentResolver cr = ctx.getContentResolver();
Cursor cursor ;
cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"),null, null, null, null);
and see new calendar in cursor. But:
it not appears in google accound
it removes after some time (guess, because of synchronization)
tested on 5.1.1
How can I create calendar to my account progrommatically?
Try using calendar.insert to create another calendar.
Since you're using Java, I think this code snippet might be of help:
import com.google.api.services.calendar.Calendar;
// ...
// Initialize Calendar service with valid OAuth credentials
Calendar service = new Calendar.Builder(httpTransport, jsonFactory, credentials)
.setApplicationName("applicationName").build();
// Create a new calendar
com.google.api.services.calendar.model.Calendar calendar = new Calendar();
calendar.setSummary("calendarSummary");
calendar.setTimeZone("America/Los_Angeles");
// Insert the new calendar
Calendar createdCalendar = service.calendars().insert(calendar).execute();
System.out.println(createdCalendar.getId());
Give the Try-it a dry run to see if it works.
I created a new calendar on android and added events.
After first sync or after a few seconds it's erasing. Can somebody help with this problem?
Code:
private void createCalendar(Context context, String accName) {
Uri calUri = CalendarContract.Calendars.CONTENT_URI;
final ContentValues v = new ContentValues();
v.put(CalendarContract.Calendars.ACCOUNT_NAME, accName);
v.put(CalendarContract.Calendars.NAME, CALENDAR_NAME);
v.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CALENDAR_NAME);
v.put(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google");
v.put(CalendarContract.Calendars.CALENDAR_COLOR, 0xEA8561);
v.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
v.put(CalendarContract.Calendars.OWNER_ACCOUNT, accName);
v.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
v.put(CalendarContract.Calendars.VISIBLE, 1);
calUri = calUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accName)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google")
.build();
calUri = calUri.buildUpon().build();
final Uri result = context.getContentResolver().insert(calUri, v);
}
Unfortinately this is a known issue with the calendar API. you have only one option if you want to use the Android calendar- create the calendar as a local one and synchronize it yourself.
I need to open Android device's Calendar app with some pre-populated data. The logic that Iam using seems to populate fields like:
Event description
Event location
From Date, To Date
All day event/not
Repeat /Recurrence Info
I am not able to populate "Reminders" section, I would like to populate Reminders section. It will great to get some help on this
Here is the code that I am using to open Calendar app and populate date.
// Intent to open Calendar Event
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(Events.CONTENT_URI);
intent.putExtra(Events.DESCRIPTION, desc);
intent.putExtra(Events.EVENT_LOCATION, location);
intent.putExtra(Events.TITLE, summary);
intent.putExtra(Events.EVENT_TIMEZONE, beginTime.getTimeZone().getID());
intent.putExtra(Events.STATUS, statusStr);
intent.putExtra(Events.VISIBLE, transparency);
intent.putExtra(Events.RRULE, "FREQ=YEARLY;INTERVAL=1;BYYEARDAY=1,2;UNTIL=20161210;");
intent.putExtra(Events.EXDATE, androidExDateStr.toString());
// Not sure on how to use CalendarContract.Reminders, Tried the following but does not seem to be working
intent.putExtra(CalendarContract.Reminders.DESCRIPTION, desc);
intent.putExtra(CalendarContract.Reminders.EVENT_LOCATION, location);
intent.putExtra(CalendarContract.Reminders.TITLE, summary);
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
intent.putExtra(CalendarContract.Reminders.DTSTART, beginTime.getTimeInMillis());
intent.putExtra(CalendarContract.Reminders.EVENT_TIMEZONE, beginTime.getTimeZone().getID());
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
intent.putExtra(CalendarContract.Reminders.DTEND, endTime.getTimeInMillis());
intent.putExtra(CalendarContract.Reminders.STATUS, statusStr);
intent.putExtra(CalendarContract.Reminders.RRULE,"FREQ=YEARLY;INTERVAL=1;BYYEARDAY=1,2;UNTIL=20161210;");
intent.putExtra(CalendarContract.Reminders.EXDATE, androidExDateStr.toString());
//intent.putExtra(CalendarContract.Reminders.METHOD, Reminders.METHOD_EMAIL);
//intent.putExtra(CalendarContract.Reminders.MINUTES, reminderVal) ;
//intent.putExtra(CalendarContract.Events.HAS_ALARM, 1);
//}
try {
context.startActivity(intent);
} catch(Exception e) {
e.printStackTrace();
Log.v(LOG_TAG, "Cannot schedule Calendar event as specified ");
return false;
}
Did you check the example from http://developer.android.com/guide/topics/providers/calendar-provider.html#reminders?
long eventID = 221;
...
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Reminders.MINUTES, 15);
values.put(Reminders.EVENT_ID, eventID);
values.put(Reminders.METHOD, Reminders.METHOD_ALERT);
Uri uri = cr.insert(Reminders.CONTENT_URI, values);
I found that Events.CONTENT_EXCEPTION_URI (here) used for make recurring event.
It's hardly to find document or code example from internet. So I try many ways
1 Insert as SyncAdapter
ContentValues values = new ContentValues();
values.put(Events.ORIGINAL_INSTANCE_TIME, CaldavGlobalVar.getCurrentTime_());
values.put(Events.SELF_ATTENDEE_STATUS, status);
if(!username.equals("")){
values.put(Events.ORGANIZER, username);
}
if(event.getSummarry()!=null){
values.put(Events.TITLE, event.getSummarry());
}
if(event.getDescription()!=null){
values.put(Events.DESCRIPTION, event.getDescription());
}
if(event.getDateStart()!=null){
values.put(Events.DTSTART, CaldavGlobalVar.convertTIMEtomilisecond(event.getDateStart(), event.getAllDay()));
}
Uri exceptionUri = Uri. withAppendedPath(Events.CONTENT_EXCEPTION_URI, String.valueOf(event.getEventId()));
Uri syncUri = CalendarProvider.asSyncAdapter(exceptionUri, username,context.getResources().getString(R.string.ACCOUNT_TYPE));
Uri resultUri = context.getContentResolver().insert(syncUri, values);
resultUri return null, I didnot see any exception or any relation things, So I dig Android source code (from here) and find out the way they use Events.CONTENT_EXCEPTION_URI So I change
2 Insert by "ContentProviderOperation" like this, in line 1003
ContentValues values = new ContentValues();
values.put(Events.ORIGINAL_INSTANCE_TIME, CaldavGlobalVar.getCurrentTime_());
values.put(Events.SELF_ATTENDEE_STATUS, 1);
values.put(Events.STATUS, Events.STATUS_CONFIRMED);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Uri exceptionUri = Uri.withAppendedPath(Events.CONTENT_EXCEPTION_URI,
String.valueOf(eventId));
ops.add(ContentProviderOperation.newInsert(exceptionUri).withValues(values).build());
mHandler.startBatch(mHandler.getNextToken(), null, CalendarContract.AUTHORITY, ops, 1000);
But it show log that It installed unsuccessfully, I am so worry about that, may be Google not support it fully, I also list all Content Provider in Android, I dont has any exception uri (Events.CONTENT_EXCEPTION_URI) --content://com.android.calendar/exception
Exception throwed
java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/exception
Does anyone have experience ? Any help are appreciate :)
Kind regards
A small part of my code:
ContentValues args = new ContentValues();
args.put(CalendarContract.Events.ORIGINAL_INSTANCE_TIME, originalinstancetime);
args.put(CalendarContract.Events.STATUS, status);
Uri.Builder eventUriBuilder = CalendarContract.Events.CONTENT_EXCEPTION_URI.buildUpon();
ContentUris.appendId(eventUriBuilder, originalEventID);
try {
final Uri resultUri = context.getContentResolver().insert(eventUriBuilder.build(), args);
int eventID = Integer.parseInt(resultUri.getLastPathSegment());
} catch (Exception e) {
}