I am using a code to add an event to the device's calendar. The code works fine in all the Android versions except Android M.The event when being added on Android M shows as a birthday instead of an event. Please help on how can this issue be fixed.
The code being used is as follows
// Add event to calendar
try {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startDateInMilliSeconds);
values.put(CalendarContract.Events.DTEND, endDateInMilliSeconds);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, description);
if (location != null && (!TextUtils.isEmpty(location))) {
values.put(CalendarContract.Events.EVENT_LOCATION, location);
}
values.put(CalendarContract.Events.CALENDAR_ID, 1);
values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance()
.getTimeZone().getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
// Save the eventId into the Task object for possible future delete.
long eventId = Long.parseLong(uri.getLastPathSegment());
Uri openCalendarEvent = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId);
Intent calIntent = new Intent(Intent.ACTION_VIEW).setData(uri);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startDateInMilliSeconds);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endDateInMilliSeconds);
calIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(calIntent);
} catch (Exception e) {
e.printStackTrace();
}
try below method # Himmat
public void addReminder() {
Intent calIntent = new Intent(Intent.ACTION_EDIT);
calIntent.setData(CalendarContract.Events.CONTENT_URI);
calIntent.setType("vnd.android.cursor.item/event");
calIntent.putExtra(CalendarContract.Events.TITLE, e.getName());
calIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, e.getLocation());
calIntent.putExtra(CalendarContract.Events.DESCRIPTION, e.getDescription());
Calendar calStart=Calendar.getInstance();
if(Validator.isNotNull(e.getStartDate())) {
calStart.setTime(e.getStartDate());
}
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
calStart.getTimeInMillis());
Calendar calEnd=Calendar.getInstance();
if(Validator.isNotNull(e.getEndDate())) {
calEnd.setTime(e.getEndDate());
}
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
calEnd.getTimeInMillis());
calIntent.putExtra(CalendarContract.EXTRA_CUSTOM_APP_URI,
e.getRegistrationLink());
startActivity(calIntent);
}
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 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.
I am doing backup and restore of calendars, During restore if calendar is not present in phone(but related account is present), i am creating new calendar with the following code, it has successfully inserted into db (i can see the return value of URI and id). But in calendar application it is visible for an second and disappears,i have no clue what is going wrong, i have made the visible flag with 1 on insertion but still not working.Can someone Help.
final String INT_NAME_PREFIX = "priv";
Uri calUri = CalendarContract.Calendars.CONTENT_URI
.buildUpon()
.appendQueryParameter(
CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(Calendars.ACCOUNT_NAME,
mCurrentMirrorItem.ACCOUNT_NAME)
.appendQueryParameter(Calendars.ACCOUNT_TYPE,
mCurrentMirrorItem.ACCOUNT_TYPE).build();
String dispName = mItem.CALENDAR_DISPLAY_NAME;
String intName = INT_NAME_PREFIX + dispName;
ContentValues contentValues = new ContentValues();
if (columnNames.contains(Calendars.ACCOUNT_NAME)) {
contentValues.put(CalendarContract.Calendars.ACCOUNT_NAME,
mItem.ACCOUNT_NAME);
}
if (columnNames.contains(Calendars.ACCOUNT_TYPE)) {
contentValues.put(CalendarContract.Calendars.ACCOUNT_TYPE,
mItem.ACCOUNT_TYPE);
}
if (columnNames.contains(Calendars.NAME)) {
contentValues.put(CalendarContract.Calendars.NAME, intName);
}
if (columnNames.contains(Calendars.CALENDAR_DISPLAY_NAME)) {
contentValues.put(
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
mItem.CALENDAR_DISPLAY_NAME);
}
if (columnNames.contains(Calendars.CALENDAR_COLOR)) {
contentValues.put(CalendarContract.Calendars.CALENDAR_COLOR,
mItem.CALENDAR_COLOR);
}
if (columnNames.contains(Calendars.CALENDAR_ACCESS_LEVEL)) {
contentValues.put(
CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,
Calendars.CAL_ACCESS_OWNER);
}
if (columnNames.contains(Calendars.OWNER_ACCOUNT)) {
contentValues.put(CalendarContract.Calendars.OWNER_ACCOUNT,
mItem.ACCOUNT_NAME);
}
if (columnNames.contains(Calendars.VISIBLE)) {
contentValues.put(CalendarContract.Calendars.VISIBLE, 1);
}
if (columnNames.contains(Calendars.SYNC_EVENTS)) {
contentValues.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
}
returnUri = cr.insert(calUri, contentValues);
long eventID = Long.parseLong(returnUri.getLastPathSegment());
calid = String.valueOf(eventID);
Log.d(tag,"calendar name: "+mItem.CALENDAR_DISPLAY_NAME+"\tNew calendar id is: "+calid+"\nInserted URI: "+returnUri);
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);
in my application I make a new calendar and add some calendar events to it.
private long addCalendar() {
Uri.Builder builder;
ContentValues values;
if (Build.VERSION.SDK_INT < 14) {
values = new ContentValues();
values.put(CalendarConst.Calendars._SYNC_ACCOUNT, calendarname);
values.put(CalendarConst.Calendars._SYNC_ACCOUNT_TYPE, "LOCAL");
values.put(CalendarConst.Calendars.NAME, calendarname);
values.put(CalendarConst.Calendars.DISPLAY_NAME, calendarname);
values.put(CalendarConst.Calendars.COLOR, calendarcolor);
values.put(CalendarConst.Calendars.ACCESS_LEVEL, CalendarConst.Calendars.OWNER_ACCESS);
values.put(CalendarConst.Calendars.TIMEZONE, "Europe/Berlin");
values.put(CalendarConst.Calendars.OWNER_ACCOUNT, getOwnerAccount());
values.put(CalendarConst.Calendars.SELECTED, 1);
values.put(CalendarConst.Calendars.SYNC_EVENTS, 1);
Uri contentUri;
if (Build.VERSION.SDK_INT >= 8) {
contentUri = Uri
.parse("content://com.android.calendar/calendars");
} else {
contentUri = Uri.parse("content://calendar/calendars");
}
builder = contentUri.buildUpon();
builder.appendQueryParameter(CalendarConst.Calendars.ACCOUNT_NAME, calendarname);
builder.appendQueryParameter(CalendarConst.Calendars.ACCOUNT_TYPE, "LOCAL");
builder.appendQueryParameter(CalendarConst.CALLER_IS_SYNCADAPTER, "true");
} else {
values = new ContentValues();
values.put(Calendars.ACCOUNT_NAME, calendarname);
values.put(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
values.put(Calendars.NAME, calendarname);
values.put(Calendars.CALENDAR_DISPLAY_NAME, calendarname);
values.put(Calendars.CALENDAR_COLOR, calendarcolor);
values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
values.put(Calendars.CALENDAR_TIME_ZONE, "Europe/Berlin");
values.put(CalendarContract.Calendars.OWNER_ACCOUNT, getOwnerAccount());
values.put(CalendarContract.Calendars.VISIBLE, 1);
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
builder = CalendarContract.Calendars.CONTENT_URI.buildUpon();
builder.appendQueryParameter(Calendars.ACCOUNT_NAME, calendarname);
builder.appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
builder.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true");
}
Uri uri = contentResolver.insert(builder.build(), values);
return Long.valueOf(uri.getLastPathSegment());
}
private long addCalendarEvent(CalendarEvent event, long calendarID) {
ContentValues values = new ContentValues();
Uri uri;
long start = Calendar.getInstance().getTimeInMillis();
if (Build.VERSION.SDK_INT < 14) {
values.put(CalendarConst.EventsColumns.DTSTART, event.getDtStart());
values.put(CalendarConst.EventsColumns.DTEND, event.getDtEnd());
values.put(CalendarConst.EventsColumns.TITLE, event.getTitle());
values.put(CalendarConst.EventsColumns.EVENT_LOCATION, event.getLocation());
values.put(CalendarConst.EventsColumns.CALENDAR_ID, calendarID);
values.put(CalendarConst.EventsColumns.EVENT_TIMEZONE, "Europe/Berlin");
values.put(CalendarConst.EventsColumns.DESCRIPTION, event.getDescription());
values.put(CalendarConst.EventsColumns.ALL_DAY, event.isAllDayEvent() ? 1 : 0);
if (!TextUtils.isEmpty(event.getrRule())) {
values.put(CalendarConst.EventsColumns.RRULE, event.getrRule());
}
uri = contentResolver.insert(CalendarConst.Events.CONTENT_URI, values);
} else {
values.put(Events.DTSTART, start);
values.put(Events.DTEND, event.getDtEnd());
values.put(Events.TITLE, event.getTitle());
values.put(Events.EVENT_LOCATION, event.getLocation());
values.put(Events.CALENDAR_ID, calendarID);
values.put(Events.EVENT_TIMEZONE, "Europe/Berlin");
values.put(Events.DESCRIPTION, event.getDescription());
values.put(Events.ALL_DAY, event.isAllDayEvent() ? 1 : 0);
if (!TextUtils.isEmpty(event.getrRule())) {
values.put(Events.RRULE, event.getrRule());
}
uri = contentResolver.insert(Events.CONTENT_URI, values);
}
return Long.valueOf(uri.getLastPathSegment());
}
But now I also want to listen for events that are added to this calendar.
How do I do this?
Make use of content observer. You can find more about content observer here or on android developers. You need to make a service that register the observer. In the onChange(); function, read the database(using content provider) from where you left last time(make use of shared preferences for that). Note all the changes.
Also pass false while registering observer using registerContentObserver if you do not want changes from subdirectories of the uri passed to call the onChange();