4

I have working in android calender. i have add event in Calender programmatically using android app. I have Also Refer this links :IllegalArgumentException: Unknown URL content://com.android.calendar/events when inserting an event to the calendar on Android Adding events to native calendar is not working but is not working in my code .

my Code is:

ContentValues contentEvent = new ContentValues();
                // Particular Calendar in which we need to add Event
                contentEvent.put("calendar_id", AlarmId);                                                  
                // Title/Caption of the Event     
                contentEvent.put("title", "Wedding");                                                           
                // Description of the Event
                contentEvent.put("description", "Wedding Party");                                  
                // Venue/Location of the Event
                contentEvent.put("eventLocation", "New York");                                                   
                // Start Date of the Event with Time  
                contentEvent.put("dtstart", l);                                                          
                // End Date of the Event with Time
                contentEvent.put("dtend", l+60*1000);                  
                // All Day Event                                       
                contentEvent.put("allDay", 1);                     
                // Set alarm for this Event                                              
                contentEvent.put("hasAlarm",1);     
                contentEvent.put("eventTimezone", android.text.format.Time.getCurrentTimezone());

                Uri eventsUri = getCalendarURI(false);                  

                // event is added successfully
                getContentResolver().insert(eventsUri, contentEvent);
//                  Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);


public Uri getCalendarURI(boolean eventUri) {
    Uri calendarURI = null;
    if (android.os.Build.VERSION.SDK_INT <= 7) {
        calendarURI = (eventUri) ? Uri.parse("content://calendar/events")
                : Uri.parse("content://calendar/calendars");
    } else {
        calendarURI = (eventUri) ? Uri
                .parse("content://com.android.calendar/events") : Uri
                .parse("content://com.android.calendar/calendars");
    }
    return calendarURI;
}

My issue Is: When i have run my application that time this error have been generated. so how can i solve this error ?

error is:

java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/

thank you in Advance

Community
  • 1
  • 1
Chirag Parmar
  • 1,064
  • 2
  • 13
  • 29

3 Answers3

3
  Calendar cal = Calendar.getInstance();  

    long l = cal.getTimeInMillis();

    long cal_Id = 1;

    **// Also Here Use Cal_Id = 1 not parse another value** 

    ContentResolver CR = getContentResolver();


     ContentValues calEvent  = new ContentValues();

     calEvent.put(CalendarContract.Events.CALENDAR_ID,  cal_Id); // XXX pick)

     calEvent.put(CalendarContract.Events.TITLE, " Demo Data");

     calEvent.put(CalendarContract.Events.DTSTART,l);

     calEvent.put(CalendarContract.Events.DTEND, l+60 * 1000);

     calEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "Indian/Christmas"); 

// Here use the proper time zone of area wise and solve this error

     Uri uri = CR.insert(URL, calEvent);                        

     int id = Integer.parseInt(uri.getLastPathSegment());                       

     Toast.makeText(this, "Created Calendar Event " + id,
            Toast.LENGTH_SHORT).show();
Chirag Parmar
  • 1,064
  • 2
  • 13
  • 29
1

try this :

Uri calendars = getCalendarURI(true);


public Uri getCalendarURI(boolean eventUri) {
    Uri calendarURI = null;
    if (android.os.Build.VERSION.SDK_INT <= 7) {
        calendarURI = (eventUri) ? Uri.parse("content://calendar/events")
                : Uri.parse("content://calendar/calendars");
    } else {
        calendarURI = (eventUri) ? Uri
                .parse("content://com.android.calendar/events") : Uri
                .parse("content://com.android.calendar/calendars");
    }
    return calendarURI;
}
Vijju
  • 3,458
  • 1
  • 22
  • 20
  • when you want calendar uri pass false as parameter o\w true for event uri – Vijju Mar 21 '14 at 12:15
  • I have also check both Boolean value but its does not work and same error generate. – Chirag Parmar Mar 21 '14 at 12:25
  • when i have put true value in getCalendarURI(true) that time java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/calendars/-447611156 This error have generate and when i have put false value in getCalendarURI(true) that time java.lang.nullpointerexeption have generate. – Chirag Parmar Mar 21 '14 at 12:49
0

You can try this way :

Calendar cal = Calendar.getInstance();              
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);

or you can find in this topic:

How to add calendar events in Android?

Mori
  • 2,653
  • 18
  • 24