Android使用Intent将事件添加到日历,获取事件ID

Android使用Intent将事件添加到日历,获取事件ID,android,android-intent,calendar,Android,Android Intent,Calendar,我试图通过日历添加事件。但是,我不知道如何获取刚刚添加的事件的事件ID Intent intent = new Intent(Intent.ACTION_EDIT); intent.setType("vnd.android.cursor.item/event"); intent.putExtra("beginTime", sdate.getTime()); intent.putExtra("endTime", edate.getTi

我试图通过日历添加事件。但是,我不知道如何获取刚刚添加的事件的事件ID

        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("beginTime", sdate.getTime());
        intent.putExtra("endTime", edate.getTime());
        intent.putExtra("allDay", true);
        intent.putExtra("rrule", "FREQ=YEARLY");
        intent.putExtra("title", "A Test Event from android app");
        intent.putExtra("description", "Description here");
        intent.putExtra("eventLocation", "location here here here");
我广泛阅读了其他资源,似乎找不到答案。我尝试了startActivityForResult,但似乎无法正常工作。在活动结束之前,我尝试的其他方法似乎无法检查它

将事件添加到日历后,是否有其他方法获取事件ID?我需要使用intent方法进行此操作。

尝试以下一种解决方案:

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;

public class CalenderUtils {

/**
 * Add a new event into a native Google calendar. Add alert notification by setting <code>isRemind</code> as <code>true</code>.
 * @param cr - ContentResolver
 * @param title - Event title
 * @param addInfo - Event description
 * @param place - Event place
 * @param status -  <code>int</code> This information is sufficient for most entries: tentative (0), confirmed (1) or canceled (2):
 * @param startDate - <code>long</code> event start time in mls
 * @param isRemind - <code>boolean</code> need to remind about event before?
 * @param isMailService - <code>boolean</code>. Adding attendees to the meeting
 * @return <code>long</code> eventID
 */
public static long addEventToCalender(ContentResolver cr, String title, String addInfo, String place, int status,
        long startDate, boolean isRemind, boolean isMailService) {
    String eventUriStr = "content://com.android.calendar/events";
    ContentValues event = new ContentValues();
    // id, We need to choose from our mobile for primary its 1
    event.put("calendar_id", 1); 
    event.put("title", title);
    event.put("description", addInfo);
    event.put("eventLocation", place);
    event.put("eventTimezone", "UTC/GMT +2:00");

    // For next 1hr
    long endDate = startDate + 1000 * 60 * 60; 
    event.put("dtstart", startDate);
    event.put("dtend", endDate);
    //If it is bithday alarm or such kind (which should remind me for whole day) 0 for false, 1 for true
    // values.put("allDay", 1);
    event.put("eventStatus", status);
    event.put("hasAlarm", 1);

    Uri eventUri = cr.insert(Uri.parse(eventUriStr), event);
    long eventID = Long.parseLong(eventUri.getLastPathSegment());

    if (isRemind) {
        String reminderUriString = "content://com.android.calendar/reminders";
        ContentValues reminderValues = new ContentValues();
        reminderValues.put("event_id", eventID);
        // Default value of the system. Minutes is a integer
        reminderValues.put("minutes", 5); 
        // Alert Methods: Default(0), Alert(1), Email(2), SMS(3)
        reminderValues.put("method", 1); 
        cr.insert(Uri.parse(reminderUriString), reminderValues); //Uri reminderUri = 
    }
    if (isMailService) {
        String attendeuesesUriString = "content://com.android.calendar/attendees";
        /********* To add multiple attendees need to insert ContentValues multiple times ***********/
        ContentValues attendeesValues = new ContentValues();
        attendeesValues.put("event_id", eventID);
        // Attendees name
        attendeesValues.put("attendeeName", "xxxxx");
        // Attendee email
        attendeesValues.put("attendeeEmail", "yyyy@gmail.com");
        // Relationship_Attendee(1), Relationship_None(0), Organizer(2), Performer(3), Speaker(4)
        attendeesValues.put("attendeeRelationship", 0);
        // None(0), Optional(1), Required(2), Resource(3)
        attendeesValues.put("attendeeType", 0);
        // None(0), Accepted(1), Decline(2), Invited(3), Tentative(4)
        attendeesValues.put("attendeeStatus", 0);
        cr.insert(Uri.parse(attendeuesesUriString), attendeesValues); //Uri attendeuesesUri = 
    }

    return eventID;
}

}
我从我的项目中复制粘贴了这个类,所以欢迎您随意更改名称。 正如您在这里注意到的,在插入后,您将收到
id

如果您需要获取与会者的提醒id或会议id,您必须处理cr.insert…中的返回值,然后处理parce uri
long id=long.parseLong(uri.getLastPathSegment())


这里还有一个附加信息:谢谢,我很久没有解决这个问题了,但这似乎也很有效@kotsumu你能详细说明你的解决方案吗?我也有这个问题。除非用户取消插入,否则不会调用onactivityResult
方法。我不能使用这里建议的,因为我也需要支持姜饼。我是否遗漏了您使用意图生成日历事件的部分?我相信这就是问题所在。如何从日历查询中获取事件id?