CRUD operations using Calendar Provider in Android
Required permissions in the manifest file.
Get all the calendars from the calendar content provider.
public void getCalendars() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_PERMISSIONS_REQUEST_WRITE_CALENDAR); return; } // Projection array. Creating indices for this array instead of doing dynamic lookups improves performance. final String[] EVENT_PROJECTION = new String[] { CalendarContract.Calendars._ID, // 0 CalendarContract.Calendars.ACCOUNT_NAME, // 1 CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, // 2 CalendarContract.Calendars.OWNER_ACCOUNT // 3 }; // The indices for the projection array above. final int PROJECTION_ID_INDEX = 0; final int PROJECTION_ACCOUNT_NAME_INDEX = 1; final int PROJECTION_DISPLAY_NAME_INDEX = 2; final int PROJECTION_OWNER_ACCOUNT_INDEX = 3; ContentResolver contentResolver = getContentResolver(); Cursor cur = contentResolver.query(CalendarContract.Calendars.CONTENT_URI, EVENT_PROJECTION, null, null, null); ArrayListcalendarInfos = new ArrayList<>(); while (cur.moveToNext()) { long calID = 0; String displayName = null; String accountName = null; String ownerName = null; // Get the field values calID = cur.getLong(PROJECTION_ID_INDEX); displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX); accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX); ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX); String calendarInfo = String.format("Calendar ID: %s\nDisplay Name: %s\nAccount Name: %s\nOwner Name: %s", calID, displayName, accountName, ownerName); calendarInfos.add(calendarInfo); } ArrayAdapter stringArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, calendarInfos); listView.setAdapter(stringArrayAdapter); }
Get the primary calendar from the calendar content provider.
public void getPrimaryCalendar() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_PERMISSIONS_REQUEST_WRITE_CALENDAR); return; } // Projection array. Creating indices for this array instead of doing dynamic lookups improves performance. final String[] EVENT_PROJECTION = new String[] { CalendarContract.Calendars._ID, // 0 CalendarContract.Calendars.ACCOUNT_NAME, // 1 CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, // 2 CalendarContract.Calendars.OWNER_ACCOUNT // 3 }; // The indices for the projection array above. final int PROJECTION_ID_INDEX = 0; final int PROJECTION_ACCOUNT_NAME_INDEX = 1; final int PROJECTION_DISPLAY_NAME_INDEX = 2; final int PROJECTION_OWNER_ACCOUNT_INDEX = 3; ContentResolver contentResolver = getContentResolver(); String selection = CalendarContract.Calendars.VISIBLE + " = 1 AND " + CalendarContract.Calendars.IS_PRIMARY + "=1"; Cursor cur = contentResolver.query(CalendarContract.Calendars.CONTENT_URI, EVENT_PROJECTION, selection, null, null); ArrayListcalendarInfos = new ArrayList<>(); while (cur.moveToNext()) { long calID = 0; String displayName = null; String accountName = null; String ownerName = null; // Get the field values calID = cur.getLong(PROJECTION_ID_INDEX); displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX); accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX); ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX); String calendarInfo = String.format("Calendar ID: %s\nDisplay Name: %s\nAccount Name: %s\nOwner Name: %s", calID, displayName, accountName, ownerName); calendarInfos.add(calendarInfo); } ArrayAdapter stringArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, calendarInfos); listView.setAdapter(stringArrayAdapter); }
Get the calendar by account from the calendar content provider.
public void readCalendarsByAccount(View view) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_PERMISSIONS_REQUEST_WRITE_CALENDAR); return; } // Projection array. Creating indices for this array instead of doing dynamic lookups improves performance. final String[] EVENT_PROJECTION = new String[] { CalendarContract.Calendars._ID, // 0 CalendarContract.Calendars.ACCOUNT_NAME, // 1 CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, // 2 CalendarContract.Calendars.OWNER_ACCOUNT // 3 }; // The indices for the projection array above. final int PROJECTION_ID_INDEX = 0; final int PROJECTION_ACCOUNT_NAME_INDEX = 1; final int PROJECTION_DISPLAY_NAME_INDEX = 2; final int PROJECTION_OWNER_ACCOUNT_INDEX = 3; // Run query Cursor cur = null; ContentResolver cr = getContentResolver(); Uri uri = CalendarContract.Calendars.CONTENT_URI; String selection = "((" + CalendarContract.Calendars.ACCOUNT_NAME + " = ?) AND (" + CalendarContract.Calendars.ACCOUNT_TYPE + " = ?) AND (" + CalendarContract.Calendars.OWNER_ACCOUNT + " = ?))"; String[] selectionArgs = new String[] {"test@gmail.com", "com.google", "test@gmail.com"}; // Submit the query and get a Cursor object back. cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null); // Use the cursor to step through the returned records ArrayListcalendars = new ArrayList<>(); while (cur.moveToNext()) { long calID = 0; String displayName = null; String accountName = null; String ownerName = null; // Get the field values calID = cur.getLong(PROJECTION_ID_INDEX); displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX); accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX); ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX); String calendarInfo = String.format("Calendar ID: %s\nDisplay Name: %s\nAccount Name: %s\nOwner Name: %s", calID, displayName, accountName, ownerName); calendars.add(calendarInfo); } ArrayAdapter stringArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, calendars); listView.setAdapter(stringArrayAdapter); }
Check if an event already exist on the calendar by event title.
private boolean isEventAlreadyExist(String eventTitle) { final String[] INSTANCE_PROJECTION = new String[] { CalendarContract.Instances.EVENT_ID, // 0 CalendarContract.Instances.BEGIN, // 1 CalendarContract.Instances.TITLE // 2 }; long startMillis = 0; long endMillis = 0; Calendar beginTime = Calendar.getInstance(); beginTime.set(2017, 11, 15, 6, 00); startMillis = beginTime.getTimeInMillis(); Calendar endTime = Calendar.getInstance(); endTime.set(2017, 11, 15, 8, 00); endMillis = endTime.getTimeInMillis(); // The ID of the recurring event whose instances you are searching for in the Instances table String selection = CalendarContract.Instances.TITLE + " = ?"; String[] selectionArgs = new String[] {eventTitle}; // Construct the query with the desired date range. Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon(); ContentUris.appendId(builder, startMillis); ContentUris.appendId(builder, endMillis); // Submit the query Cursor cur = getContentResolver().query(builder.build(), INSTANCE_PROJECTION, selection, selectionArgs, null); return cur.getCount() > 0; }
Add an event to the calendar through calendar content provider.
public void addEvent() { String eventTitle = "Jazzercise"; if (isEventAlreadyExist(eventTitle)) { Snackbar.make(view, "Jazzercise event already exist!", Snackbar.LENGTH_SHORT).show(); return; } long calID = 3; long startMillis = 0; long endMillis = 0; Calendar beginTime = Calendar.getInstance(); beginTime.set(2017, 11, 15, 6, 00); startMillis = beginTime.getTimeInMillis(); Calendar endTime = Calendar.getInstance(); endTime.set(2017, 11, 15, 8, 00); endMillis = endTime.getTimeInMillis(); ContentResolver cr = getContentResolver(); ContentValues values = new ContentValues(); values.put(CalendarContract.Events.DTSTART, startMillis); values.put(CalendarContract.Events.DTEND, endMillis); values.put(CalendarContract.Events.TITLE, "Jazzercise"); values.put(CalendarContract.Events.DESCRIPTION, "Group workout"); values.put(CalendarContract.Events.CALENDAR_ID, calID); values.put(CalendarContract.Events.EVENT_TIMEZONE, "America/Los_Angeles"); values.put(CalendarContract.Events.ORGANIZER, "google_calendar@gmail.com"); if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED) { Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values); long eventID = Long.parseLong(uri.getLastPathSegment()); Log.i("Calendar", "Event Created, the event id is: " + eventID); Snackbar.make(view, "Jazzercise event added!", Snackbar.LENGTH_SHORT).show(); } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_PERMISSIONS_REQUEST_WRITE_CALENDAR); } }
Remove an event from calendar by event title and time through calendar content provider.
public void removeEvent(View view) { String eventTitle = "Jazzercise"; final String[] INSTANCE_PROJECTION = new String[] { CalendarContract.Instances.EVENT_ID, // 0 CalendarContract.Instances.BEGIN, // 1 CalendarContract.Instances.TITLE // 2 }; // The indices for the projection array above. final int PROJECTION_ID_INDEX = 0; final int PROJECTION_BEGIN_INDEX = 1; final int PROJECTION_TITLE_INDEX = 2; // Specify the date range you want to search for recurring event instances Calendar beginTime = Calendar.getInstance(); beginTime.set(2017, 9, 23, 8, 0); long startMillis = beginTime.getTimeInMillis(); Calendar endTime = Calendar.getInstance(); endTime.set(2018, 1, 24, 8, 0); long endMillis = endTime.getTimeInMillis(); // The ID of the recurring event whose instances you are searching for in the Instances table String selection = CalendarContract.Instances.TITLE + " = ?"; String[] selectionArgs = new String[] {eventTitle}; // Construct the query with the desired date range. Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon(); ContentUris.appendId(builder, startMillis); ContentUris.appendId(builder, endMillis); // Submit the query Cursor cur = getContentResolver().query(builder.build(), INSTANCE_PROJECTION, selection, selectionArgs, null); while(cur.moveToNext()) { // Get the field values long eventID = cur.getLong(PROJECTION_ID_INDEX); long beginVal = cur.getLong(PROJECTION_BEGIN_INDEX); String title = cur.getString(PROJECTION_TITLE_INDEX); Uri deleteUri = null; deleteUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID); int rows = getContentResolver().delete(deleteUri, null, null); Log.i("Calendar", "Rows deleted: " + rows); } }
Get events from calendar by event title through calendar content provider.
private void showEvents(String eventTitle) { final String[] INSTANCE_PROJECTION = new String[] { CalendarContract.Instances.EVENT_ID, // 0 CalendarContract.Instances.BEGIN, // 1 CalendarContract.Instances.TITLE, // 2 CalendarContract.Instances.ORGANIZER //3 }; // The indices for the projection array above. final int PROJECTION_ID_INDEX = 0; final int PROJECTION_BEGIN_INDEX = 1; final int PROJECTION_TITLE_INDEX = 2; final int PROJECTION_ORGANIZER_INDEX = 3; // Specify the date range you want to search for recurring event instances Calendar beginTime = Calendar.getInstance(); beginTime.set(2017, 9, 23, 8, 0); long startMillis = beginTime.getTimeInMillis(); Calendar endTime = Calendar.getInstance(); endTime.set(2018, 1, 24, 8, 0); long endMillis = endTime.getTimeInMillis(); // The ID of the recurring event whose instances you are searching for in the Instances table String selection = CalendarContract.Instances.TITLE + " = ?"; String[] selectionArgs = new String[] {eventTitle}; // Construct the query with the desired date range. Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon(); ContentUris.appendId(builder, startMillis); ContentUris.appendId(builder, endMillis); // Submit the query Cursor cur = getContentResolver().query(builder.build(), INSTANCE_PROJECTION, selection, selectionArgs, null); ArrayListevents = new ArrayList<>(); while (cur.moveToNext()) { // Get the field values long eventID = cur.getLong(PROJECTION_ID_INDEX); long beginVal = cur.getLong(PROJECTION_BEGIN_INDEX); String title = cur.getString(PROJECTION_TITLE_INDEX); String organizer = cur.getString(PROJECTION_ORGANIZER_INDEX); // Do something with the values. Log.i("Calendar", "Event: " + title); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(beginVal); DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); Log.i("Calendar", "Date: " + formatter.format(calendar.getTime())); events.add(String.format("Event: %s\nOrganizer: %s\nDate: %s", title, organizer, formatter.format(calendar.getTime()))); } ArrayAdapter stringArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, events); listView.setAdapter(stringArrayAdapter); }
Show all events from calendar in a time range using calendar content provider.
public void readEvents(View view) { final String[] INSTANCE_PROJECTION = new String[] { CalendarContract.Instances.EVENT_ID, // 0 CalendarContract.Instances.BEGIN, // 1 CalendarContract.Instances.TITLE, // 2 CalendarContract.Instances.ORGANIZER }; // The indices for the projection array above. final int PROJECTION_ID_INDEX = 0; final int PROJECTION_BEGIN_INDEX = 1; final int PROJECTION_TITLE_INDEX = 2; final int PROJECTION_ORGANIZER_INDEX = 3; // Specify the date range you want to search for recurring event instances Calendar beginTime = Calendar.getInstance(); beginTime.set(2017, 9, 23, 8, 0); long startMillis = beginTime.getTimeInMillis(); Calendar endTime = Calendar.getInstance(); endTime.set(2018, 1, 24, 8, 0); long endMillis = endTime.getTimeInMillis(); // The ID of the recurring event whose instances you are searching for in the Instances table String selection = CalendarContract.Instances.EVENT_ID + " = ?"; String[] selectionArgs = new String[] {"207"}; // Construct the query with the desired date range. Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon(); ContentUris.appendId(builder, startMillis); ContentUris.appendId(builder, endMillis); // Submit the query Cursor cur = getContentResolver().query(builder.build(), INSTANCE_PROJECTION, null, null, null); ArrayListevents = new ArrayList<>(); while (cur.moveToNext()) { // Get the field values long eventID = cur.getLong(PROJECTION_ID_INDEX); long beginVal = cur.getLong(PROJECTION_BEGIN_INDEX); String title = cur.getString(PROJECTION_TITLE_INDEX); String organizer = cur.getString(PROJECTION_ORGANIZER_INDEX); // Do something with the values. Log.i("Calendar", "Event: " + title); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(beginVal); DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); Log.i("Calendar", "Date: " + formatter.format(calendar.getTime())); events.add(String.format("Event: %s\nOrganizer: %s\nDate: %s", title, organizer, formatter.format(calendar.getTime()))); } ArrayAdapter stringArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, events); listView.setAdapter(stringArrayAdapter); }
Update an event from calendar by event id.
private void updateEvent(long eventID) { ContentResolver cr = getContentResolver(); ContentValues values = new ContentValues(); values.put(CalendarContract.Events.TITLE, "Kickboxing"); Uri updateUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID); int rows = getContentResolver().update(updateUri, values, null, null); Log.i("Calendar", "Rows updated: " + rows); }
Delete an event from calendar by event id.
private void deleteEvent(long eventID) { Uri deleteUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID); int rows = getContentResolver().delete(deleteUri, null, null); Log.i("Calendar", "Rows deleted: " + rows); }
Reference:
https://developer.android.com/guide/topics/providers/calendar-provider.html
Search within Codexpedia
Search the entire web