获取基于事件ID的日历事件详细信息,Android吗?

时间:2018-10-04 07:20:56

标签: android calendar

我使用以下代码将事件保存在设备日历中

ContentResolver cr = getCurrentContext().getContentResolver();
                ContentValues values = new ContentValues();

                Calendar startDate = Calendar.getInstance();
                startDate.setTimeInMillis(contentDatum.getGist().getScheduleStartDate());
                Calendar endDate = Calendar.getInstance();
                endDate.setTimeInMillis(contentDatum.getGist().getScheduleEndDate());

                values.put(CalendarContract.Events.DTSTART, startDate.getTimeInMillis());
                values.put(CalendarContract.Events.DTEND, endDate.getTimeInMillis());
                values.put(CalendarContract.Events.TITLE, contentDatum.getGist().getTitle());
                values.put(CalendarContract.Events.DESCRIPTION, contentDatum.getGist().getDescription());

                values.put(CalendarContract.Events.CALENDAR_ID, getCalendarId());
                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);
                Intent intent = new Intent(Intent.ACTION_EDIT);
                intent.setData(ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID));
                getCurrentActivity().startActivityForResult(intent, 0);

我正在保存插入时返回的eventID。

要基于事件ID检索数据,我正在使用以下代码

for (int i = 0; i < calendarEventIds.length; i++) {
                        long eventID = Long.parseLong(calendarEventIds[i]);
                        Uri event = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
                        Cursor cursor;
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                            cursor = getCurrentContext().getContentResolver().query(event, null, null, null);
                        } else {
                            cursor = getCurrentActivity().managedQuery(event, null, null, null, null);
                        }

                        if (cursor.getCount() == 1) {
                            if (cursor.moveToFirst()) {
                                if (cursor.getLong(cursor.getColumnIndex(CalendarContract.Events.DTSTART)) == contentDatum.getGist().getScheduleStartDate()
                                        && cursor.getLong(cursor.getColumnIndex(CalendarContract.Events.DTEND)) == contentDatum.getGist().getScheduleEndDate()
                                        && cursor.getString(cursor.getColumnIndex(CalendarContract.Events.TITLE)).equalsIgnoreCase(contentDatum.getGist().getTitle())
                                        && cursor.getString(cursor.getColumnIndex(CalendarContract.Events.DESCRIPTION)).equalsIgnoreCase(contentDatum.getGist().getDescription())) {

                                    Toast.makeText(getCurrentContext(), "Event already exists in your calendar.", Toast.LENGTH_SHORT).show();
                                    calendarEventExist = true;
                                }
                            }
                        }
                    }

但是我没有添加活动的详细信息。

1 个答案:

答案 0 :(得分:0)

我将日历ID保存在逗号分隔的字符串中。下面的代码可以很好地获取事件的详细信息

if (getCalendarEventId() != null) {
                    String[] calendarEventIds = getCalendarEventId().split(";");
                    for (int i = 0; i < calendarEventIds.length; i++) {
                        long eventID = Long.parseLong(calendarEventIds[i]);
                        Uri event = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
                        Cursor cursor;
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                            cursor = getCurrentContext().getContentResolver().query(event, null, null, null);
                        } else {
                            cursor = getCurrentActivity().managedQuery(event, null, null, null, null);
                        }
                        if (cursor.moveToFirst()) {
                            do {
                                if ((cursor.getInt(cursor.getColumnIndex(CalendarContract.Events.DELETED)) == 1)) {
//The added event was deleted and is not visible in calendar but exists in calendar DB

                                    calendarEventExist = false;
                                    break;
                                }
                                if (cursor.getString(cursor.getColumnIndex(CalendarContract.Events.TITLE)).equalsIgnoreCase(contentDatum.getGist().getTitle())
                                        && cursor.getString(cursor.getColumnIndex(CalendarContract.Events.DESCRIPTION)).equalsIgnoreCase(contentDatum.getGist().getDescription())) {
                                    Toast.makeText(getCurrentContext(), "Event already exists in your calendar.", Toast.LENGTH_SHORT).show();
                                    calendarEventExist = true;
                                    break;
                                }
                            } while (cursor.moveToNext());

                        }
                    }
                } else {
                    calendarEventExist = false;
                }
相关问题