代码之家  ›  专栏  ›  技术社区  ›  WISHY

根据事件id获取日历事件详细信息,android?

  •  0
  • WISHY  · 技术社区  · 6 年前

    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 回复  |  直到 6 年前
        1
  •  0
  •   WISHY    6 年前

    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;
                    }