创建新的日历同步与Android API日历、Android、API

2023-09-05 08:59:57 作者:苍凉小巷

我想创建我的帐户的日历,以填补我从一些网站获取事件。我已经搜查,发现我已经修改,以获得什么,我需要一些新的Andr​​oid 4.0日历例子。的问题是,日历被创建,填充有事件,但不同步与谷歌日程表,因此在未来同步它被擦除。我用的是funcion是这些:

i'm trying to create a calendar on my account to fill with events that i get from some websites. I've searched and found some new android 4.0 calendar example that i've modified to obtain what i need. The problem is that the calendar is created, filled with events but not synced with google calendar, so in the next sync it is erased. The funcion i use are these:

这是一个用于添加新日历,如果不alreay存在:

This is the one for add the new calendar if don't alreay exist:

public static Uri createCalendarWithName(Context ctx, String name,String accountName) {

            Uri target = Uri.parse(CalendarContract.Calendars.CONTENT_URI.toString());
            target = target.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accountName)
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google").build();

            ContentValues values = new ContentValues();
            values.put(Calendars.ACCOUNT_NAME, accountName);
            values.put(Calendars.ACCOUNT_TYPE, "com.google");
            values.put(Calendars.NAME, name);
            values.put(Calendars.CALENDAR_DISPLAY_NAME, name);
            values.put(Calendars.CALENDAR_COLOR, 0x00FF00);
            values.put(Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_ROOT);
            values.put(Calendars.OWNER_ACCOUNT, accountName);
            values.put(Calendars.VISIBLE, 1);
            values.put(Calendars.SYNC_EVENTS, 1);
            values.put(Calendars.CALENDAR_TIME_ZONE, "Europe/Rome");
            values.put(Calendars.CAN_PARTIALLY_UPDATE, 1);
            values.put(Calendars.CAL_SYNC1, "https://www.google.com/calendar/feeds/" + accountName + "/private/full");
            values.put(Calendars.CAL_SYNC2, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + accountName);
            values.put(Calendars.CAL_SYNC3, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + accountName);
            values.put(Calendars.CAL_SYNC4, 1);
            values.put(Calendars.CAL_SYNC5, 0);
            values.put(Calendars.CAL_SYNC8, System.currentTimeMillis());

            Uri newCalendar = ctx.getContentResolver().insert(target, values);

            return newCalendar;
    }

和一个创建一个没有互动的新事件:

and that one create the new event without interaction:

public static Uri createEventWithName(Context ctx, long id, String name, String data) {
        long startMillis = 0; 
        long endMillis = 0;     
        int id2=(int)id;
        String[] divisi = data.split("/");
        Calendar beginTime = Calendar.getInstance();
        beginTime.set(2012,Integer.parseInt(divisi[0])-1, Integer.parseInt(divisi[1]));
        startMillis = beginTime.getTimeInMillis();
        Calendar endTime = Calendar.getInstance();
        endTime.set(2012,Integer.parseInt(divisi[0])-1, Integer.parseInt(divisi[1]));
        endMillis = endTime.getTimeInMillis();


        ContentValues cv = new ContentValues();
        cv.put(Events.TITLE, name);
        cv.put(Events.DTSTART, startMillis);
        cv.put(Events.DTEND, endMillis);
        cv.put(Events.CALENDAR_ID, id2);
        Log.d("aggiungo a calendario",Integer.toString(id2));
        cv.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().toString());
        //cv.put(Events.RRULE, "FREQ=DAILY;INTERVAL=2");

        Uri newEvent = ctx.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, cv);
        return newEvent;
}

我没有那么在Android的编程经历,所以我认为这是一个愚蠢的问题=)我读过的一个存储在Android设备,否则事件被取消帐户名和帐户类型必须是相同的。我得到帐户名来自Android的API,我认为他们是正确的。帐户类型似乎适用于其他......

I'm not so experienced in Android programming so i think it's a stupid question =) i've read that accountName and Account Type must be the same that the one stored on android device, else the event is cancelled. I get accountName from android api and i think they are correct. The account type seems to work for other....

由于任何人说救我!

推荐答案

从 HTTP ://developer.android.com/reference/android/provider/CalendarContract.html

公共静态最后弦乐ACCOUNT_TYPE_LOCAL

public static final String ACCOUNT_TYPE_LOCAL

加在API级别14,一种特殊的日历帐户类型不   与任何帐户相关联。通常情况下日历不匹配   帐户的设备将被删除。设置上的ACCOUNT_TYPE   日历这将$ P $从pvent它被消灭,如果不匹配   现有帐户。

Added in API level 14 A special account type for calendars not associated with any account. Normally calendars that do not match an account on the device will be removed. Setting the account_type on a calendar to this will prevent it from being wiped if it does not match an existing account.

另请参阅ACC​​OUNT_TYPE常数值:本地

See Also ACCOUNT_TYPE Constant Value: "LOCAL"