简体   繁体   中英

Inserting Calendar items in Android ICS

For one of my application we have to insert an event into calendar.

long calID = 3;
long startMillis = 0; 
long endMillis = 0;     
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 8, 10, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 8, 10, 8, 45);
endMillis = endTime.getTimeInMillis();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Jazzercise");
values.put(Events.DESCRIPTION, "Group workout");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
Uri uri = cr.insert(CalendarContract.Calendars.CONTENT_URI, values);
// get the event ID that is the last element in the Uri
long eventID = Long.parseLong(uri.getLastPathSegment());
Log.d("MainActivity", "addCalendarEvents :: " + "eventID :: "+eventID);

Cursor cursor = cr.query(Events.CONTENT_URI, null, Events.TITLE +"='Jazzercise'", null, null);
Log.d("MainActivity", "addCalendarEvents :: " + "cursor :: "+cursor.getCount());

Provided on http://developer.android.com/guide/topics/providers/calendar-provider.html However, firstly it gives me a error

 Failed to get type for: content://com.android.calendar (Unknown URL content://com.android.calendar)

Also the cursor count is zero. When i try to search with the title. Note: I tried using the intents service to add events, however I don't want user discretion while adding the event.

I have tested it on a Galaxy Nexus (4.1) and Nexus S(4.1).

Any help with the correct UI which is to be used with ICS?

BR, Jayshil

I've figured it out. The line:

Failed to get type for: content://com.android.calendar (Unknown URL content://com.android.calendar)

is a complete red herring (ie irrelevant). It's simply some framework code that performs infrastructural work, as seen here .


The problem lies in the documentation - if you're like me, you probably expected long calID = 3; to be some valid Calendar ID. It's not a valid ID, it's a placeholder for the sake of documentation .

What you're doing with the code above, if you're using an empty emulator, is inserting an entry into a non-existent calendar, ie nowhere visible.

What you need is to:

  1. Query for and choose an existing calendar . Once again - on the emulator, there is no default calendar .
  2. Use the obtained actual calendar ID to insert the event.

In short - the documentation and debug messages are not idiot-proof enough ;).

Do you have the correct permissions in the Manifest file?

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

That might be part of the cause of the error you're seeing.

content://com.android.calendar - problem is here.

Its calenders.

Try with this code:

Uri mCalendarsUri;
if (Build.VERSION.SDK_INT >= 8) {
    mCalendarsUri = Uri.parse("content://com.android.calendar/calendars");
} else {
    mCalendarsUri = Uri.parse("content://calendar/calendars");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM