I'm trying to add events to the basic Calendar by using Intent. I think it's working fine but when I try to read it I always got an exception. java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/
If I'm using this URL: "content://com.android.calendar/calendars"
my ReadFromCalendar()
method shows me two IDs but non of them is correct and if I add a new event the number of the IDs doesn't change. So I guess it's not the correct URL.
If I'm using this URL:"content://com.android.calendar/" + "events"
then I got IDs from the lowest value to the highest value. When I add a new event the IDs value growing with +1 no matter what set to ID.
So the quesiton is how can I get the correct eventID-s from my calendar? Am I using the wrong URL?
Here is the what I'm using:
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;
}
}