0

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?

Community
  • 1
  • 1
Siarhei
  • 2,358
  • 3
  • 27
  • 63

1 Answers1

1

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.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56