Make exception event from original recurring event

2019-01-24 21:48发布

问题:

I found that Events.CONTENT_EXCEPTION_URI (here) used for make recurring event. It's hardly to find document or code example from internet. So I try many ways

1 Insert as SyncAdapter

ContentValues values = new ContentValues();
    values.put(Events.ORIGINAL_INSTANCE_TIME, CaldavGlobalVar.getCurrentTime_());
    values.put(Events.SELF_ATTENDEE_STATUS, status);
    if(!username.equals("")){
        values.put(Events.ORGANIZER, username);
    }
    if(event.getSummarry()!=null){
        values.put(Events.TITLE, event.getSummarry());
    }
    if(event.getDescription()!=null){
        values.put(Events.DESCRIPTION, event.getDescription());
    }
    if(event.getDateStart()!=null){
        values.put(Events.DTSTART, CaldavGlobalVar.convertTIMEtomilisecond(event.getDateStart(), event.getAllDay()));
    }


   Uri exceptionUri = Uri. withAppendedPath(Events.CONTENT_EXCEPTION_URI,  String.valueOf(event.getEventId()));
    Uri syncUri = CalendarProvider.asSyncAdapter(exceptionUri, username,context.getResources().getString(R.string.ACCOUNT_TYPE));

    Uri resultUri = context.getContentResolver().insert(syncUri, values);

resultUri return null, I didnot see any exception or any relation things, So I dig Android source code (from here) and find out the way they use Events.CONTENT_EXCEPTION_URI So I change

2 Insert by "ContentProviderOperation" like this, in line 1003

ContentValues values = new ContentValues();
    values.put(Events.ORIGINAL_INSTANCE_TIME, CaldavGlobalVar.getCurrentTime_());
    values.put(Events.SELF_ATTENDEE_STATUS, 1);
    values.put(Events.STATUS, Events.STATUS_CONFIRMED);

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    Uri exceptionUri = Uri.withAppendedPath(Events.CONTENT_EXCEPTION_URI,
            String.valueOf(eventId));
    ops.add(ContentProviderOperation.newInsert(exceptionUri).withValues(values).build());

    mHandler.startBatch(mHandler.getNextToken(), null, CalendarContract.AUTHORITY, ops, 1000);

But it show log that It installed unsuccessfully, I am so worry about that, may be Google not support it fully, I also list all Content Provider in Android, I dont has any exception uri (Events.CONTENT_EXCEPTION_URI) --content://com.android.calendar/exception

Exception throwed

java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/exception

Does anyone have experience ? Any help are appreciate :)

Kind regards

回答1:

A small part of my code:

ContentValues args = new ContentValues();
args.put(CalendarContract.Events.ORIGINAL_INSTANCE_TIME, originalinstancetime);
args.put(CalendarContract.Events.STATUS, status);
Uri.Builder eventUriBuilder = CalendarContract.Events.CONTENT_EXCEPTION_URI.buildUpon();
ContentUris.appendId(eventUriBuilder, originalEventID);
try {
    final Uri resultUri = context.getContentResolver().insert(eventUriBuilder.build(), args);
    int eventID = Integer.parseInt(resultUri.getLastPathSegment());
} catch (Exception e) {
}