Insert calendar items in Android ICS

For one of my applications, we need to insert an event into the 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()); 

Courtesy of http://developer.android.com/guide/topics/providers/calendar-provider.html However, firstly, it gives me an 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 by name. Note. I tried to use the intent service to add events, however I do not want the user to discretion when adding the event.

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

Any help with the right user interface to be used with ICS?

BR, Jayshil

+4
source share
3 answers

I get it. Line:

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

- complete red herring (i.e. irrelevant). It’s just some kind of wireframe code that does the infrastructure work, as you can see here .


The problem is the documentation - if you are like me, you probably expected long calID = 3; will be the correct Calendar id. This is not a valid identifier; this is a placeholder for documentation .

What you do with the code above, if you use an empty emulator, inserts an entry into a nonexistent calendar, i.e. not visible anywhere.

What you need:

In short, the documentation and debugging messages are not idiotic enough;).

+3
source

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" /> 

This may be the cause of the error you are seeing.

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

His calendars.

Try using 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"); } 
0
source

Source: https://habr.com/ru/post/1433346/


All Articles