同步事件以编程方式添加到Android中的谷歌日历

时间:2012-01-18 23:18:01

标签: android google-calendar-api

我正在尝试向Android日历中添加一个事件,并指定该事件将添加到gmail日历中,以便自动与Google日历同步。 问题是以编程方式添加的事件不会与Google日历同步,但如果我在手机上添加手动,它会与Google日历同步。我不知道为什么。

这是我用来添加事件的代码:

ArrayList<MyCalendar> calendars = new ArrayList<MyCalendar>();

String[] projection = new String[] { "_id", "name" };
Uri calUri = getCalendarURI(false);
Cursor managedCursor = managedQuery(calUri, projection, "selected=1",
        null, null);

String calName = null;
String calId = null;
if (managedCursor.moveToFirst()) {

    int nameColumn = managedCursor.getColumnIndex("name");
    int idColumn = managedCursor.getColumnIndex("_id");
    do {
        calName = managedCursor.getString(nameColumn);
        calId = managedCursor.getString(idColumn);
        calendars.add(new MyCalendar(Integer.parseInt(calId), calName));
    } while (managedCursor.moveToNext());

}
Toast.makeText(getBaseContext(), calName + "  " + calId,
        Toast.LENGTH_LONG).show();

Calendar cal = Calendar.getInstance();
ContentValues event = new ContentValues();
event.put("calendar_id", 2);
event.put("title", "Test Event2");
event.put("description", "Hiii Buddy");
long startTime = cal.getTimeInMillis();
long endTime = cal.getTimeInMillis() + 60 * 60 * 1000;
event.put("dtstart", startTime);
event.put("dtend", endTime);
event.put("allDay", 0);
event.put("eventStatus", 1);// tentative 0, confirmed 1 canceled 2
event.put("visibility", 3);// default 0 confidential 1 private 2
                            // public 3
event.put("transparency", 0);// opaque 0 transparent 1
event.put("hasAlarm", 1); // 0 false, 1 true

Uri eventsUri = getCalendarURI(true);
Uri url = getContentResolver().insert(eventsUri, event);

因此,该活动已成功添加到日历中,但它并未显示在网络上的Google日历中(不同步),但如果我手动添加该活动,则会同步!

3 个答案:

答案 0 :(得分:3)

您可以在使用此功能添加事件后同步事件,这对我有用(在API 8及更高版本中):

public static void syncCalendar(Context context, String calendarId) {
        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(getCalendarUri(),
                        Long.parseLong(calendarId)), values, null, null);
    }

答案 1 :(得分:2)

尝试使用此代码将事件插入到Android日历以及Google日历中:

 ContentValues values = new ContentValues();

   cal_id = String.valueOf(p1);
    values.put("calendar_id", p1);
    values.put("title", title1);
    values.put("allDay", 0);
    values.put("dtstart", settime); 
    values.put("dtend", cal.getTimeInMillis()+60*60*1000); 
    values.put("description", desc1);
    values.put("visibility", 0);
    values.put("transparency", 0);
    values.put("hasAttendeeData", 1);
    values.put("hasAlarm", 0);
    event = cr.insert(EVENTS_URI, values);
    event1=event; 
    dat1 = event.toString();
    long id=-1;
    if (event != null) 
    {
         id = Long.parseLong(event.getLastPathSegment());
         ContentValues values1 = new ContentValues();
         values1.put("event_id", id);
         values1.put("method", 1); //METHOD_ALERT

        Uri reminder = Uri.parse(getCalendarUriBase(this) + "reminders");
        this.getContentResolver().insert(reminder, values1);
        if(s.length() > 0 || partmail.length() > 0)
        {
     //REQUIRES FOLLOWING CODE 

此代码用于将事件同步添加到Google日历

             ContentValues attendees = new ContentValues();
             attendees.put("event_id", id);
             attendees.put("attendeeEmail", partmail1);
             attendees.put("attendeeRelationship", 2);//RELATIONSHIP_ATTENDEE
             attendees.put("attendeeStatus", 3); //ATTENDEE_STATUS_INVITED       
             attendees.put("attendeeType", 1); //TYPE_REQUIRED

            id1=(int)id;
            alarmid = (int) id;
            Uri attendeesUri = null;
            if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
            {
                     attendeesUri = Uri.parse("content://com.android.calendar/attendees");
            }
            else if(Integer.parseInt(Build.VERSION.SDK) < 8)
            {
                    attendeesUri = Uri.parse("content://calendar/attendees");
            }
            this.getContentResolver().insert(attendeesUri, attendees);

            Toast.makeText(this, "Task Scheduled Successfully", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(this, "Could not create Task!", Toast.LENGTH_SHORT);               
        }


    // reminder insert
    Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
    values = new ContentValues();
    values.put( "event_id", id);
    values.put( "method", 1 );
    values.put( "minutes", 0 );
    cr.insert( REMINDERS_URI, values );

获取日历的Uri,因为它的API级别不同,例如直到8,大于8和大于11。

private String getCalendarUriBase(Context con) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
    managedCursor = managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
    // eat
}

if (managedCursor != null) {
    calendarUriBase = "content://calendar/";
} else {
    calendars = Uri.parse("content://com.android.calendar/calendars");
    try {
        managedCursor = managedQuery(calendars, null, null, null, null);
    } catch (Exception e) {
        // statement to print the stacktrace
    }

    if (managedCursor != null) {
        calendarUriBase = "content://com.android.calendar/";
    }

}

return calendarUriBase;

}

答案 2 :(得分:1)

使用提供的{comder api here

上述参考文献有点笼统,因为Android没有提供官方API。所以,另一个更具特定于Android的解决方案是An Android Tutorial–Programming with Calendar

其中也包含一个工作示例。另外,请注意权限声明,如READ_CALENDAR,WRITE_CALENDAR