Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calendar event saved locally on phone, but can't sync to Google Calendar

I try to add a calendar by the following code. The event created can be read by calendar apps on my phone, but just sync to online Google Calendar. Can any one give me some hits to solve the issue?

here are some remark 1. Code was run on my real phone (Galaxy Nexus 4.1.1) 2. All other calendar events can sync to Google Calendar, just the program-added cannot be sync.

---update---

When I change the follow code

values.put(CalendarContract.Events.SYNC_EVENTS,1);
values.put(CalendarContract.Events.VISIBLE, 1);

-- I get the error

java.lang.IllegalArgumentException: Only the provider may write to sync_events

    public void addEvent() {

    long startMillis = 0;
    long endMillis = 0;

    Log.v("LOG", "entered addEvent");


    //Calendar beginTime = Calendar.getInstance();
    //beginTime.set(2012, 8, 11, 22, 0);
    //startMillis = beginTime.getTimeInMillis();
    startMillis = System.currentTimeMillis() + (3600 * 1000)*4;

    //Calendar endTime = Calendar.getInstance();
    //endTime.set(2012, 8, 11, 23, 0);
    //endMillis = endTime.getTimeInMillis();
    endMillis = System.currentTimeMillis() + (3600 * 1000)*5;

    ContentResolver cr = getContentResolver();
    ContentValues values = new ContentValues();

    values.put(CalendarContract.Events.DTSTART, startMillis);
    values.put(CalendarContract.Events.DTEND, endMillis);
    values.put(CalendarContract.Events.TITLE, "Dog");
    values.put(CalendarContract.Events.DESCRIPTION, "DogInDESCRIPTION");
    values.put(CalendarContract.Events.CALENDAR_ID, 1);
    values.put(CalendarContract.Events.EVENT_TIMEZONE, "eventTimezone");

    values.put(CalendarContract.Events.SYNC_EVENTS,0);      


    cr.insert(CalendarContract.Events.CONTENT_URI, values);
}
like image 865
Steven Hui Avatar asked Aug 12 '12 03:08

Steven Hui


1 Answers

You shouldn't set CalendarContract.Events.SYNC_EVENTS on CalendarContract.Events.CONTENT_URI.

If the calendar is already setup as visible and synchronzed on your device, you can just add an event, and it will synchronize.

If you need to turn the sync on from the app you can set CalendarContract.Events.SYNC_EVENTS on CalendarContract.Calendars.CONTENT_URI.

For example (calId is the id of the calendar you want to update to be visible and synchronise).

ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
values.put(CalendarContract.Calendars.VISIBLE, 1);

cr.update(ContentUris.withAppendedId(Calendars.CONTENT_URI, calId), values, null, null);
like image 192
Ben Gardner Avatar answered Sep 29 '22 12:09

Ben Gardner