Вопрос

I'm using the following code to insert an event (Programmatically, not by intent) to specific calendar (with it's ID)

        ContentResolver cr = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put(CalendarContract.Events.DTSTART, startMillis);
        values.put(CalendarContract.Events.DTEND, endMillis);
        values.put(CalendarContract.Events.TITLE, cname);
        values.put(CalendarContract.Events.DESCRIPTION, note);
        values.put(CalendarContract.Events.CALENDAR_ID, calID);
        values.put(CalendarContract.Events.EVENT_TIMEZONE, tz.getDisplayName());
        if (recurrence != null)
            values.put(CalendarContract.Events.RRULE, "FREQ=" + recurrence + ";");
        Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

Insert works great, i'm saving the event id: uri.getLastPathSegment()

but the issue is how to read this specific event from the specific calendar ?

Thanks for the help

Это было полезно?

Решение

You can use the contentResolver to query CalendarContract.Events with a selection on the eventId

To fetch the calendarId it's something like this:

Uri calUri = CalendarContract.Calendars.CONTENT_URI.buildUpon()
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, mAccountName)
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, mAccountType)
            .build();

getContentResolver().query(calUri, new String[]{CalendarContract.Calendars._ID}, null, null, null);

Другие советы

you can put insert reminder and event in calender by this way :

Calendar cal = Calendar.getInstance();     
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(New_Quote_Activity.this) + "events");
ContentResolver cr1 = getContentResolver();

// event insert
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", "Text");
values.put("dtstart", cal.getTimeInMillis() + 1*60*1000); // event starts at 11 minutes from now
values.put("dtend", cal.getTimeInMillis()+5760*60*1000); // ends 60 minutes from now
values.put("description", edt_Location.getText().toString());
// values.put("visibility", 0);
values.put("hasAlarm", 1);
values.put("eventTimezone", Time.getCurrentTimezone());
Uri event = cr1.insert(EVENTS_URI, values);

// reminder insert
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(New_Quote_Activity.this) + "reminders");
values = new ContentValues();
values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
values.put( "method", 1 );
values.put( "minutes", 2880 );
cr1.insert( REMINDERS_URI, values );

For reading event use this code :

Cursor cursor = cr.query(Uri.parse("content://calendar/events"), new String[]{ "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation" }, null, null, null);         
    //Cursor cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "name" }, null, null, null);
    String add = null;
    cursor.moveToFirst();
    String[] CalNames = new String[cursor.getCount()];
    int[] CalIds = new int[cursor.getCount()];
    for (int i = 0; i < CalNames.length; i++) {
        CalIds[i] = cursor.getInt(0);
        CalNames[i] = "Event"+cursor.getInt(0)+": \nTitle: "+ cursor.getString(1)+"\nDescription: "+cursor.getString(2)+"\nStart Date: "+new Date(cursor.getLong(3))+"\nEnd Date : "+new Date(cursor.getLong(4))+"\nLocation : "+cursor.getString(5);
        if(add == null)
            add = CalNames[i];
        else{
            add += CalNames[i];
        }           
        ((TextView)findViewById(R.id.calendars)).setText(add);

        cursor.moveToNext();
    }
    cursor.close();

Also, don't forgot to add this permissions :

<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top