将 3 天内发生的所有事件从日历读入我的 android 应用程序
Reading all the events happened during 3 days from the calendar into my android app
我正在尝试在 activity 中记录从今天起 3 天内发生的所有事件。我收到以下错误。请帮忙。
我的代码:
public static long getStartOfDayInMillis() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
public static ArrayList<String> readCalendarEvent(Context context) {
Uri.Builder eventsUriBuilder;
eventsUriBuilder = Uri.parse("content://com.android.calendar/events").buildUpon();
ContentUris.appendId(eventsUriBuilder,getStartOfDayInMillis());
ContentUris.appendId(eventsUriBuilder, getStartOfDayInMillis() + 3*(24 * 60 * 60 * 1000));
Uri eventsUri = eventsUriBuilder.build();
Cursor cursor = context.getContentResolver()
.query(
eventsUri,
new String[] { "calendar_id", "title", "description",
"dtstart", "dtend", "eventLocation" }, null,
null, CalendarContract.Instances.DTSTART + " ASC");
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
// fetching calendars id
nameOfEvent.clear();
startDates.clear();
endDates.clear();
descriptions.clear();
for (int i = 0; i < CNames.length; i++) {
nameOfEvent.add(cursor.getString(1));
startDates.add(getDate(Long.parseLong(cursor.getString(3))));
// endDates.add(getDate(Long.parseLong(cursor.getString(4))));
descriptions.add(cursor.getString(2));
CNames[i] = cursor.getString(1);
cursor.moveToNext();
}
return nameOfEvent;
}
我调用它是为了在 Java class 中提供所有事件名称的日志作为..
results= readCalendarEvent(MyCalendarActivity.this);
for(int i=0;i< results.size();i++ )
Log.d("results",results.get(i));
我收到以下错误:
Caused by: java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/events/1448821800000/1448908200000
这是因为你用错了URL。替换为:
eventsUriBuilder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
或者:
eventsUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon()
我正在尝试在 activity 中记录从今天起 3 天内发生的所有事件。我收到以下错误。请帮忙。 我的代码:
public static long getStartOfDayInMillis() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
public static ArrayList<String> readCalendarEvent(Context context) {
Uri.Builder eventsUriBuilder;
eventsUriBuilder = Uri.parse("content://com.android.calendar/events").buildUpon();
ContentUris.appendId(eventsUriBuilder,getStartOfDayInMillis());
ContentUris.appendId(eventsUriBuilder, getStartOfDayInMillis() + 3*(24 * 60 * 60 * 1000));
Uri eventsUri = eventsUriBuilder.build();
Cursor cursor = context.getContentResolver()
.query(
eventsUri,
new String[] { "calendar_id", "title", "description",
"dtstart", "dtend", "eventLocation" }, null,
null, CalendarContract.Instances.DTSTART + " ASC");
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
// fetching calendars id
nameOfEvent.clear();
startDates.clear();
endDates.clear();
descriptions.clear();
for (int i = 0; i < CNames.length; i++) {
nameOfEvent.add(cursor.getString(1));
startDates.add(getDate(Long.parseLong(cursor.getString(3))));
// endDates.add(getDate(Long.parseLong(cursor.getString(4))));
descriptions.add(cursor.getString(2));
CNames[i] = cursor.getString(1);
cursor.moveToNext();
}
return nameOfEvent;
}
我调用它是为了在 Java class 中提供所有事件名称的日志作为..
results= readCalendarEvent(MyCalendarActivity.this);
for(int i=0;i< results.size();i++ )
Log.d("results",results.get(i));
我收到以下错误:
Caused by: java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/events/1448821800000/1448908200000
这是因为你用错了URL。替换为:
eventsUriBuilder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
或者:
eventsUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon()