我正在尝试使用Intent将事件添加到基本日历中。我认为它工作正常,但当我尝试阅读它时,我总是有例外。 java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/
如果我正在使用此网址:"content://com.android.calendar/calendars"
我的ReadFromCalendar()
方法向我显示了两个ID但其中没有ID是正确的,如果我添加新事件,则ID的数量不会更改。所以我猜这不是正确的网址。
如果我正在使用此网址:"content://com.android.calendar/" + "events"
,那么我会从最低值到最高值获取ID。当我添加新事件时,无论设置为ID,ID值都会以+1增长。
问题是如何从我的日历中获取正确的eventID-s?我使用了错误的网址吗?
以下是我正在使用的内容:
public class CalendarUtils {
static int numC = 0;
private final Activity activity;
public CalendarUtils(Activity activity) {
this.activity = activity;
}
public void InsertAnEvent() {
numC++;
long event_id = numC;
Calendar calendarEvent = Calendar.getInstance();
Intent i = new Intent(Intent.ACTION_EDIT);
i.setType("vnd.android.cursor.item/event");
i.putExtra("beginTime", calendarEvent.getTimeInMillis());
i.putExtra("allDay", true);
i.putExtra("_id", event_id);
i.putExtra("rule", "FREQ=YEARLY");
i.putExtra("endTime", calendarEvent.getTimeInMillis() + 60 * 60 * 1000);
i.putExtra("title", "Eskuvo");
activity.startActivity(i);
}
public void ReadFromCalendar() {
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(activity));
ContentResolver cr = activity.getContentResolver();
Cursor cursor;
cursor = cr.query(EVENTS_URI, null, null, null, null);
while (cursor.moveToNext()) {
long id = cursor.getLong(cursor.getColumnIndex("_id"));
Log.d("TAG", "ID: " + id);
if (id == 63) {
Toast.makeText(activity, "győztél", Toast.LENGTH_SHORT).show();
}
}
cursor.close();
}
public static String getCalendarUriBase(Activity activity) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = activity.getContentResolver().query(calendars,
null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
managedCursor = activity.getContentResolver().query(calendars,
null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
return calendarUriBase;
}
}