3

我想在日历中添加事件,我可以添加帐户,但我不知道如何与日历同步。

我应该创建一个新的日历 ID 吗?如何验证事件是在日历上创建的?

4

2 回答 2

2

查看此Google 代码项目,它将为您提供指导。

在该项目中,有一种方法可以为您提供与日历关联的帐户 ID 的 ID。

private int ListSelectedCalendars() {
        int result = 0;
        String[] projection = new String[] { "_id", "name" };
        String selection = "selected=1";
        String path = "calendars";

        Cursor managedCursor = getCalendarManagedCursor(projection, selection,
                path);

        if (managedCursor != null && managedCursor.moveToFirst()) {

            Log.i(DEBUG_TAG, "Listing Selected Calendars Only");

            int nameColumn = managedCursor.getColumnIndex("name");
            int idColumn = managedCursor.getColumnIndex("_id");

            do {
                String calName = managedCursor.getString(nameColumn);
                String calId = managedCursor.getString(idColumn);
                Log.i(DEBUG_TAG, "Found Calendar '" + calName + "' (ID="
                        + calId + ")");
                // You have to give email id in below line, right now i puted my email id
                if (calName != null && calName.contains("vimalrajpara2006@gmail.com")) {
                    result = Integer.parseInt(calId);
                }
            } while (managedCursor.moveToNext());
        } else {
            Log.i(DEBUG_TAG, "No Calendars");
        }

        return result;

    }

基于该 ID,您还可以添加事件以及搜索。

private Uri MakeNewCalendarEntry(int calId/*Value received from ListSelectedCalendars function*/) {
        ContentValues event = new ContentValues();

        event.put("calendar_id", calId);
        event.put("title", "Today's Event [TEST]");
        event.put("description", "2 Hour Presentation");
        event.put("eventLocation", "Online");

        long startTime = System.currentTimeMillis() + 1000 * 60 * 60;
        long endTime = System.currentTimeMillis() + 1000 * 60 * 60 * 2;

        event.put("dtstart", startTime);
        event.put("dtend", endTime);

        event.put("allDay", 0); // 0 for false, 1 for true
        event.put("eventStatus", 1);
        event.put("visibility", 0);
        event.put("transparency", 0);
        event.put("hasAlarm", 0); // 0 for false, 1 for true

        Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");

        Uri insertedUri = getContentResolver().insert(eventsUri, event);
        return insertedUri;
    }

有关更多功能,请查看上面提到的 Google 代码项目。

于 2012-08-20T07:38:56.147 回答
2

这对我有用,记得我正在使用 android 4.0

 void createCalendar(Datahelper dh, Context mContext, Account account) 
      { 
        final ContentValues v = new ContentValues(); 
            v.put(CalendarContract.Calendars.NAME,account.name);
            v.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, account.name);                  
        v.put(CalendarContract.Calendars.ACCOUNT_NAME, account.name);       
        v.put(CalendarContract.Calendars.ACCOUNT_TYPE, account.type); 
        v.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.GREEN); 
        v.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,Calendars.CAL_ACCESS_OWNER);         
        v.put(CalendarContract.Calendars.OWNER_ACCOUNT, account.name); 
        v.put(CalendarContract.Calendars._ID, 123);// u can give any id there and use same id any where u need to create event   
        v.put(Calendars.SYNC_EVENTS, 1); 
        v.put(Calendars.VISIBLE, 1); 
        Uri creationUri = asSyncAdapter(Calendars.CONTENT_URI, account.name, account.type); 
        Uri calendarData = mContext.getContentResolver().insert(creationUri, v); 
        long id = Long.parseLong(calendarData.getLastPathSegment()); 
       } 
    private Uri asSyncAdapter(Uri uri, String account, String accountType) 
        { 
        return uri.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true").appendQueryParameter                  (Calendars.ACCOUNT_NAME,account) .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType) .build(); 
        } 
        ContentValues values = new ContentValues();
        String eventTitle = eventsubject[i]; 
        values.put(Events.DTSTART, startMillis); 
        values.put(Events.HAS_ALARM, 1); 
        values.put(Events.DTEND, endMillis); 
        values.put(Events.EVENT_COLOR, Color.BLUE); 
        values.put(Events.TITLE, eventTitle); 
        values.put(Events.DESCRIPTION, ""); 
        values.put(Events.CALENDAR_ID, calendarId1); 
        values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault() .getID()); 
        Uri uri = cr.insert(Events.CONTENT_URI, values); 
        long eventID = Long.parseLong(uri.getLastPathSegment()); 
        ContentValues reminders = new ContentValues(); 
        reminders.put(Reminders.EVENT_ID, eventID); 
        reminders.put(Reminders.METHOD, Reminders.METHOD_ALERT); 
        reminders.put(Reminders.MINUTES, 2); 
        Uri uri2 = cr.insert(Reminders.CONTENT_URI, reminders); 
        Log.e("Reminder", "addreminder" + uri2);  
于 2012-09-17T06:21:15.010 回答