How can I add a new phone calendar to Android?

前端 未结 4 1111
刺人心
刺人心 2020-12-24 04:02

The calendar app on Android can merge multiple separate calendars: local phone calendar, Google calendars, etc.

How can I add a new such calendar to the Android phon

4条回答
  •  没有蜡笔的小新
    2020-12-24 04:11

    I just managed to add a Calendar using the new API from ICS. Stefan's answer helped, but to be able to write to all columns, you have to do the operation as a sync adapter. You can do this from a normal app using CalendarContract.ACCOUNT_TYPE_LOCAL. The following code should work on ICS. Maybe some column values are not right. Check out the documentation of CalendarContract.Calendars. Unfortunately I cannot help you for older Android versions, but CalendarContract is only used for some constant string values. Maybe these also work on older versions.

    Uri calUri = CalendarContract.Calendars.CONTENT_URI;
    ContentValues cv = new ContentValues();
    cv.put(CalendarContract.Calendars.ACCOUNT_NAME, yourAccountName);
    cv.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
    cv.put(CalendarContract.Calendars.NAME, yourInternalName);
    cv.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, yourDisplayName);
    cv.put(CalendarContract.Calendars.CALENDAR_COLOR, yourColor);
    cv.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
    cv.put(CalendarContract.Calendars.OWNER_ACCOUNT, true);
    cv.put(CalendarContract.Calendars.VISIBLE, 1);
    cv.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
    
    calUri = calUri.buildUpon()
        .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, ACCOUNT_NAME)
        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
        .build();
    Uri result = this.getContentResolver().insert(calUri, cv);
    

提交回复
热议问题