我正在使用以下代码在我的
中添加一个事件ContentResolver cr = getCurrentContext().getContentResolver();
ContentValues values = new ContentValues();
Calendar startDate = Calendar.getInstance();
startDate.setTimeInMillis(1538677837930L);
Calendar endDate = Calendar.getInstance();
endDate.setTimeInMillis(1538674237930L);
values.put(CalendarContract.Events.DTSTART, startDate.getTimeInMillis());
values.put(CalendarContract.Events.DTEND, endDate.getTimeInMillis());
values.put(CalendarContract.Events.TITLE, "event test");
values.put(CalendarContract.Events.DESCRIPTION, "event desc");
values.put(CalendarContract.Events.CALENDAR_ID, 3);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
long eventID = Long.parseLong(uri.getLastPathSegment());
Log.e("event", "" + eventID);
setCalendarEventId(eventID);
Toast.makeText(getCurrentContext(), contentDatum.getGist().getTitle() + " added to your calendar.", Toast.LENGTH_SHORT).show();
但是我确实收到了日历事件ID。但是该事件未显示在日历应用中。
我正在使用OS 7的Samsung设备上运行代码。有什么解决方案?
答案 0 :(得分:0)
结果表明,默认日历ID可能因设备而异。 首先必须使用以下代码以编程方式获取日历ID 私人诠释getCalendarId(){
Cursor cursor = null;
ContentResolver contentResolver = getCurrentActiveContext().getContentResolver();
Uri calendars = CalendarContract.Calendars.CONTENT_URI;
String[] EVENT_PROJECTION = new String[]{
CalendarContract.Calendars._ID, // 0
CalendarContract.Calendars.ACCOUNT_NAME, // 1
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, // 2
CalendarContract.Calendars.OWNER_ACCOUNT, // 3
CalendarContract.Calendars.IS_PRIMARY // 4
};
int PROJECTION_ID_INDEX = 0;
int PROJECTION_ACCOUNT_NAME_INDEX = 1;
int PROJECTION_DISPLAY_NAME_INDEX = 2;
int PROJECTION_OWNER_ACCOUNT_INDEX = 3;
int PROJECTION_VISIBLE = 4;
cursor = contentResolver.query(calendars, EVENT_PROJECTION, null, null, null);
if (cursor.moveToFirst()) {
String calName;
long calId = 0;
String visible;
do {
calName = cursor.getString(PROJECTION_DISPLAY_NAME_INDEX);
calId = cursor.getLong(PROJECTION_ID_INDEX);
visible = cursor.getString(PROJECTION_VISIBLE);
Log.e("Calendar Id : ", "" + calId + " : " + calName + " : " + visible);
if (visible.equals("1")) {
return (int) calId;
}
} while (cursor.moveToNext());
return (int) calId;
}
return 1;
}
下面的代码仅供参考,以了解存在多少个日历及其ID。
String projection[] = {"_id", "calendar_displayName"};
Uri calendars;
calendars = Uri.parse("content://com.android.calendar/calendars");
ContentResolver contentResolver = getCurrentContext().getContentResolver();
Cursor managedCursor = contentResolver.query(calendars, projection, null, null, null);
String calID="1";
if (managedCursor.moveToFirst()){
String calName;
int cont= 0;
int nameCol = managedCursor.getColumnIndex(projection[1]);
int idCol = managedCursor.getColumnIndex(projection[0]);
do {
calName = managedCursor.getString(nameCol);
calID = managedCursor.getString(idCol);
Log.e("calName",""+calName);
Log.e("calID",""+calID);
cont++;
} while(managedCursor.moveToNext());
managedCursor.close();
}