Hello I am a beginner in android.
I want to add an event in google calendar from my android application.
Please help me with complete source code. I have looked, but haven't found a solution with complete source code.
You can use ContentValues like local db,
String eventUriString = "content://com.android.calendar/events";
ContentResolver cr = this.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.CALENDAR_ID, 1);
values.put(CalendarContract.Reminders.TITLE, "YOUR_TITLE");
values.put(CalendarContract.Reminders.DTSTART, TIMES IN MILLISECOND);
values.put(CalendarContract.Reminders.DESCRIPTION,YOUR_TEXT);
values.put(CalendarContract.Reminders.DTEND, TIMES IN MILLISECOND);
values.put(CalendarContract.Reminders.HAS_ALARM, true);
values.put(CalendarContract.Reminders.EVENT_TIMEZONE, TimeZone.getDefault().getID());
Uri uriEvents = cr.insert(CalendarContract.Events.CONTENT_URI, values);
eventID = Long.parseLong(uriEvents.getLastPathSegment());
Related
I have an issue related to calendar. I am using ContentResolver to add event to calendar. It works pretty well on any devices accept of samsung devices. When I am trying to add event to original samsung calendar nothing happens. But when I downloaded google calendar event added properly to it. Is samsung calendar is regular calendar and how can i use it like primary calendar? Thanks in advance for any help.
private long insertEventTask(Activity activity, CalEvent calEvent) {
ContentResolver contentResolver = activity.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, calEvent.getStartMillis());
values.put(CalendarContract.Events.DTEND, calEvent.getEndMillis());
values.put(CalendarContract.Events.TITLE, calEvent.getTitle());
values.put(CalendarContract.Events.DESCRIPTION, calEvent.getDescription());
values.put(CalendarContract.Events.CALENDAR_ID, calEvent.getCalId());
values.put(CalendarContract.Events.EVENT_TIMEZONE, calEvent.getTimezone());
#SuppressLint("MissingPermission")
Uri uri = contentResolver.insert(CalendarContract.Events.CONTENT_URI, values);
Log.e("xxx", "uri is " + uri);
long eventId = Long.parseLong(uri.getLastPathSegment());
return eventId;
}
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 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 made a mistake on testing insert Events using CalendarContract.
I set my own _ID in a Events insert.
values.put(Events._ID, "156498713465");
Now, all my new events are created with a bad id (for exemple -535191590).
When I click to the event in the Google Calendar Application, it crash.
I have the same error as this thread :
Calendar corrupted in Android
I tried to delete all bad events :
activity.getContentResolver().delete(Events.CONTENT_URI, Events._ID + " > ? ",
new String[] { "10000" });
But when a new events are inserted, a bad id is generated.
My question is :
Where can I reset the Events Id sequence ?
Thanks,
Regards
Don't set the id; the following will do what you want:
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put (Events.CALENDAR_ID, Long.toString(newCalendarId));
values.put (Events.DTSTART, dtStart);
values.put (Events.DTEND, dtEnd);
values.put (Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
values.put (Events.TITLE, title);
Uri uri = cr.insert (Events.CONTENT_URI, values);
// The returned uri will contain the eventId assigned by Events.
I'm trying to implement my first android Program. It should write calendar entries (I know, not the best task to begin programming Andorid).
I've tried:
Uri CALENDAR_URI = Uri.parse("content://calendar/events");
ContentResolver cr = getContentResolver();
cr.delete(CALENDAR_URI, null, null); // Delete all
cr.delete(CALENDAR_URI, "calendar_id=1", null); // Delete all in default calendar
cr.delete(CALENDAR_URI, "_id=1", null); // Delete specific entry
Nothing worked. I allays get a "cannot delete that URL".
Inserting an Calendar Entry was simple:
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", this.title);
values.put("allDay", this.allDay);
values.put("dtstart", this.dtstart.toMillis(false));
values.put("dtend", this.dtend.toMillis(false));
values.put("description", this.description);
values.put("eventLocation", this.eventLocation);
values.put("visibility", this.visibility);
values.put("hasAlarm", this.hasAlarm);
cr.insert(CALENDAR_URI, values);
According to my insert method accessing the calendar worked.
Thanks, Arthur!
OK, one thing I didn't try:
Uri CALENDAR_URI = Uri.parse("content://calendar/events");
int id = 1; // calendar entry ID
Uri uri = ContentUris.withAppendedId(CALENDAR_URI, id);
cr.delete(uri, null, null);
This is what I was missing:
Uri uri = ContentUris.withAppendedId(CALENDAR_URI, id);
should lead to content://calendar/events/1
Now my Calendar is empty :-)
The right way to delete things out of a user's calendar is to use the appropriate GData APIs and delete it from their Google Calendar. Manipulating the Calendar application's content provider -- as you are trying to do -- is not part of the public API.