在Android中以编程方式同步事件添加到Google日历中 [英] sync event added programmatically with google calendar in android

查看:177
本文介绍了在Android中以编程方式同步事件添加到Google日历中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将事件添加到android日历,并且我指定将事件添加到gmail日历,以便自动与Google日历同步. 问题在于,以编程方式添加的事件无法与Google日历同步,但是如果我手动在电话上添加事件,则它会与Google日历同步.我不知道为什么.

I am trying to add an event to the android calendar, and I specify that the event will be added to the gmail calendar in order to sync with the Google calendar automatically. The problem is events added programmatically don't sync with Google calendar, but if I add it manual on the phone it does sync with Google calendar. I don't know why.

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

This is the code that I use to add the event:

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日历中(不同步),但是如果我手动添加该事件,它将进行同步!

So the event successfully added to calendar but it doesn't show up in the Google calendar at the web (don't sync) but if I add the event manually it does sync !!!

推荐答案

您可以通过此函数添加事件后对其进行同步,它对我有用(在API 8及更高版本中):

You can sync your event after adding it by this function,It's worked for me(in API 8 and later):

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);
    }

这篇关于在Android中以编程方式同步事件添加到Google日历中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆