我在android日历中工作过。我使用Android应用程序以编程方式在Calender中添加事件。 我也请参考此链接: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 但是我的代码没有工作。
我的代码是:
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;
}
我的问题是:当我运行我的应用程序时,已经生成了此错误。那我怎么能解决这个错误呢?
错误是:
java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/
先谢谢你
答案 0 :(得分: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");
//这里使用区域的正确时区并解决此错误
Uri uri = CR.insert(URL, calEvent);
int id = Integer.parseInt(uri.getLastPathSegment());
Toast.makeText(this, "Created Calendar Event " + id,
Toast.LENGTH_SHORT).show();
答案 1 :(得分:1)
试试这个:
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;
}
答案 2 :(得分:0)
你可以试试这个:
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);
<块引用>
或者您可以在本主题中找到: