I am creating event on Google calendar from my application.
Somehow it didn't work from one day to an other, it is not inserting it when I request to add the event.
I get the following error :
02-04 20:32:36.975: I/CalendarProvider2(20633): Sending notification
intent: Intent { act=android.intent.action.PROVIDER_CHANGED
dat=content://com.android.calendar }
ERROR :
02-04 20:32:36.975: W/ContentResolver(20633): Failed to get type for:
content://com.android.calendar (Unknown URL
content://com.android.calendar)
Here is my code :
//create note in google calendar
long calID = Long.parseLong(calendarId);
TimeZone timeZone = TimeZone.getDefault();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DURATION, "PT1H");
values.put(Events.TITLE, content);
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, timeZone.getID());
Uri uri = cr.insert(Events.CONTENT_URI, values);
// get the event ID that is the last element in the Uri
idSync = (int) Long.parseLong(uri.getLastPathSegment());
Related
I have to invite multiple members to the google calendar while creating the event and later on when event is created then he can alter the invited member from google calendar .
Here is the code to insert the multiple invitee : but can't able to insert the attendee. but i am able to insert the remaining data.
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
ArrayList<String> al = new ArrayList<String>();
al.add("ashis#gmail.com");
al.add("sonam#gmail.com");
intent.putStringArrayListExtra(CalendarContract.Attendees.ATTENDEE_EMAIL,al);
intent.putExtra(CalendarContract.Events.EVENT_LOCATION, locationName);
Update google Calendar : Here i am able to update the value of the endTime but attendee is not been updated .
public void updateEvent(long eventID,long endTime,String calendarEventID){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("content://com.android.calendar/events/" + calendarEventID));
ContentResolver cr = getActivity().getContentResolver();
Uri eventUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.DTEND, endTime);
event.put(CalendarContract.Attendees.ATTENDEE_EMAIL, "sunil#gmail.com");
cr.update(eventUri, event, null, null);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(intent);
}
According to the Android docs in Adding Attendees, you ought to use put instead of like:
Here is an example that adds a single attendee to an event. Note that the EVENT_ID is required:
long eventID = 202;
...
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Attendees.ATTENDEE_NAME, "Trevor");
values.put(Attendees.ATTENDEE_EMAIL, "trevor#example.com");
values.put(Attendees.ATTENDEE_RELATIONSHIP, Attendees.RELATIONSHIP_ATTENDEE);
values.put(Attendees.ATTENDEE_TYPE, Attendees.TYPE_OPTIONAL);
values.put(Attendees.ATTENDEE_STATUS, Attendees.ATTENDEE_STATUS_INVITED);
values.put(Attendees.EVENT_ID, eventID);
Uri uri = cr.insert(Attendees.CONTENT_URI, values
);
I'm adding events to a local calendar, this works fine with API < 24 (KitKat, Lollipop, Marshmallow), but I get issues with Google Calendar not able to open the events from my local calendar and returning "The requested event was not found"
Events are listed into Google Calendar, but cannot be opened, edited or deleted
Code to create the local calendar:
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "My Calendar");
values.put(CalendarContract.Calendars.VISIBLE, 1);
values.put(CalendarContract.Calendars.NAME, "My Calendar");
values.put(CalendarContract.Calendars.CALENDAR_COLOR, BLACK_COLOR);
Uri updateUri = CalendarContract.Calendars.CONTENT_URI;
updateUri.buildUpon()
.appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER, "false")
.build();
Uri uri = cr.insert(updateUri, values);
Code to create an event into the calendar:
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, start);
values.put(CalendarContract.Events.DTEND, end);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, description);
values.put(CalendarContract.Events.CALENDAR_ID, calID); // CalID = My Calendar Id
values.put(CalendarContract.Events.EVENT_TIMEZONE, "Australia/Sydney");
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
You need to provide an account name CalendarContract.Calendars.ACCOUNT_NAME and an account type in the ContentValues when you are creating the calendar
I'm using the following function to add an event to calendar:
public String addEventToCalendar(long startDate, long endDate, String recurrenceRule, boolean isAllDay, String title, String description, String location, long calendarID) {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
TimeZone timeZone = TimeZone.getDefault();
values.put(CalendarContract.Events.DTSTART, startDate);
values.put(CalendarContract.Events.DTEND, endDate);
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
if (recurrenceRule != null)
values.put(CalendarContract.Events.RRULE, recurrenceRule);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, description);
values.put(CalendarContract.Events.CALENDAR_ID, calendarID);
values.put(CalendarContract.Events.ALL_DAY, isAllDay);
values.put(CalendarContract.Events.EVENT_LOCATION, location);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
return null; // we don't have the right permissions
}
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
String eventID = uri.getLastPathSegment();
return eventID;
}
It works, but the resulting calendar event, have a 30 minutes reminder! I'm not able to figure out why. Any clue please?
Thanks a lot.
It seems like a default reminder is added after you insert your calendar event.
What you could try is to check if there are any reminder associated with your event right after you inserted it. And delete it if so.
CalendarContract.Reminders.query(contentResolver, eventId, projection)
will give you a list of reminder associated with the eventId
If the Cursor contains any reminder you can delete it with :
getContentResolver().delete(ContentUris.withAppendedId(CalendarContract.Reminders.CONTENT_URI, reminderId), null, null);
docs : http://developer.android.com/reference/android/provider/CalendarContract.Reminders.html
Try to add this
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.HAS_ALARM, 0);
Whether the event has an alarm or not. Column name.
Type: INTEGER (boolean)
public static final String HAS_ALARM = "hasAlarm";
I have created some events and stored the data in sqlite table but not the system calendar. However, i would like to have an alert/reminder for these events, which is similar to other events that stored in the system.
With reference to the code below, the details of the event are put in the "CalendarAlerts" table and in the broadcast receiver, the alertCursor is used to find the event data with start time close to the current time.
The code worked well if the event is stored in the system with a "long" eventID. But when i try to put data of my sqlite event in "CalendarAlerts" with a "string" eventID. It shows that the data is inserted into the table successfully but i would not query back the result in the "Alert Receiver" class.
Searched google for a while and it seems that not many people are talking on the topic of "CalendarAlerts". Great if anyone would share the experience on this issue.
Setting the AlarmManager
Uri alertUri = CalendarAlerts.CONTENT_URI;
long alarmMillis = (long) mStart - (long)(min*60*1000);
ContentValues alertValues =AlertUtils.makeContentValues(eventIdentifier,mStart, mEnd,alarmMillis, 0);
context.getContentResolver().insert(alertUri, alertValues);
public static ContentValues makeContentValues(String eventId, long begin, long end,
long alarmTime, int minutes) {
ContentValues values = new ContentValues();
values.put(CalendarAlerts.EVENT_ID, eventId);
values.put(CalendarAlerts.BEGIN, begin);
values.put(CalendarAlerts.END, end);
values.put(CalendarAlerts.ALARM_TIME, alarmTime);
long currentTime = System.currentTimeMillis();
values.put(CalendarAlerts.CREATION_TIME, currentTime);
values.put(CalendarAlerts.RECEIVED_TIME, 0);
values.put(CalendarAlerts.NOTIFY_TIME, 0);
values.put(CalendarAlerts.STATE, CalendarAlerts.STATE_SCHEDULED);
values.put(CalendarAlerts.MINUTES, minutes);
return values;
}
AlertReceiver.java
Cursor alertCursor = cr.query(CalendarAlerts.CONTENT_URI, ALERT_PROJECTION,
(ACTIVE_ALERTS_SELECTION + currentMillis), ACTIVE_ALERTS_SELECTION_ARGS,
ACTIVE_ALERTS_SORT);
You can use this code which works for me :
ContentResolver cr = getActivity().getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, "description");
TimeZone timeZone = TimeZone.getDefault();
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
// default calendar
values.put(CalendarContract.Events.CALENDAR_ID, 1);
//for one hour
values.put(CalendarContract.Events.DURATION, "+P1H");
values.put(CalendarContract.Events.HAS_ALARM, 1);
// cr.delete(CalendarContract.Events.CONTENT_URI, null,null);
// insert event to calendar
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
I'um using CalendarContract to insert a custom local (not synced) calendar and events. It's working on all devices, but not on Sony Xperia (4.4.4). Code for inserting a new local calendar
final ContentValues cv = new ContentValues();
cv.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
cv.put(CalendarContract.Calendars.ACCOUNT_NAME, calendarName);
cv.put(CalendarContract.Calendars.NAME, calendarName);
cv.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, calendarDisplayName);
cv.put(CalendarContract.Calendars.CALENDAR_COLOR, 0xeeff0000);
cv.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_READ);
cv.put(CalendarContract.Calendars.VISIBLE, 1);
//cv.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
Uri calUri = CalendarContract.Calendars.CONTENT_URI
.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, calendarName)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
.build();
//context.getContentResolver().insert(calUri, cv);
try {
Uri result = context.getContentResolver().insert(calUri, cv);
Long calId = Long.parseLong(result.getLastPathSegment());
Log.d(MainActivity.DEBUG_KEY, "==> add Calendar OK: " + calId);
return calId;
} catch (Exception exception) {
Log.d(MainActivity.DEBUG_KEY, "==> add Calendar FAILED: " + exception.getMessage());
}
The calendarName ist the app package name. Code for inserting Events:
long eventID = 0;
TimeZone timeZone = TimeZone.getDefault();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startDate.getTime());
values.put(CalendarContract.Events.DTEND, endDate.getTime());
values.put(CalendarContract.Events.ALL_DAY, allDay ? 1 : 0);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, description);
values.put(CalendarContract.Events.CALENDAR_ID, calendarId);
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
try {
Uri uri = context.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, values);
eventID = Long.parseLong(uri.getLastPathSegment());
Log.d(DEBUG_KEY, "EVENT ADDED: " + eventID);
} catch (Exception exception) {
Log.d(DEBUG_KEY, "EVENT-ADDED FAILED: " + exception.getMessage());
}
There are no errors oder exceptions. The debug console display the correct inserted calendar and event id. On all devices i can see, display and edit the events in the calendar app.
On Sony Xperia devices the events are showing in the calendar app. But tapping on the event for showing/editing, the calendar app crahes with the follwing error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.calendar/com.android.calendar.EventInfoActivity}: java.lang.NullPointerException
...
Reading a NULL string not supported here.