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

前端之家收集整理的这篇文章主要介绍了Android使用Intent将事件添加到日历,获取EventID前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试通过日历意图添加事件.但是,我无法弄清楚如何获取刚刚添加的事件的事件ID.
  1. Intent intent = new Intent(Intent.ACTION_EDIT);
  2. intent.setType("vnd.android.cursor.item/event");
  3. intent.putExtra("beginTime",sdate.getTime());
  4. intent.putExtra("endTime",edate.getTime());
  5. intent.putExtra("allDay",true);
  6. intent.putExtra("rrule","FREQ=YEARLY");
  7. intent.putExtra("title","A Test Event from android app");
  8. intent.putExtra("description","Description here");
  9. intent.putExtra("eventLocation","location here here here");

我在其他资源上广泛阅读,似乎无法找到答案.我尝试了startActivityForResult,但似乎无法让它工作.我尝试的其他方法似乎无法在活动结束前检查它.

在将事件添加到日历后,是否还有其他方法可以获取事件ID?我需要使用intent方法.

解决方法

试试这个解决方案:
  1. import android.content.ContentResolver;
  2. import android.content.ContentValues;
  3. import android.net.Uri;
  4.  
  5. public class CalenderUtils {
  6.  
  7. /**
  8. * Add a new event into a native Google calendar. Add alert notification by setting <code>isRemind</code> as <code>true</code>.
  9. * @param cr - ContentResolver
  10. * @param title - Event title
  11. * @param addInfo - Event description
  12. * @param place - Event place
  13. * @param status - <code>int</code> This information is sufficient for most entries: tentative (0),confirmed (1) or canceled (2):
  14. * @param startDate - <code>long</code> event start time in mls
  15. * @param isRemind - <code>boolean</code> need to remind about event before?
  16. * @param isMailService - <code>boolean</code>. Adding attendees to the meeting
  17. * @return <code>long</code> eventID
  18. */
  19. public static long addEventToCalender(ContentResolver cr,String title,String addInfo,String place,int status,long startDate,boolean isRemind,boolean isMailService) {
  20. String eventUriStr = "content://com.android.calendar/events";
  21. ContentValues event = new ContentValues();
  22. // id,We need to choose from our mobile for primary its 1
  23. event.put("calendar_id",1);
  24. event.put("title",title);
  25. event.put("description",addInfo);
  26. event.put("eventLocation",place);
  27. event.put("eventTimezone","UTC/GMT +2:00");
  28.  
  29. // For next 1hr
  30. long endDate = startDate + 1000 * 60 * 60;
  31. event.put("dtstart",startDate);
  32. event.put("dtend",endDate);
  33. //If it is bithday alarm or such kind (which should remind me for whole day) 0 for false,1 for true
  34. // values.put("allDay",1);
  35. event.put("eventStatus",status);
  36. event.put("hasAlarm",1);
  37.  
  38. Uri eventUri = cr.insert(Uri.parse(eventUriStr),event);
  39. long eventID = Long.parseLong(eventUri.getLastPathSegment());
  40.  
  41. if (isRemind) {
  42. String reminderUriString = "content://com.android.calendar/reminders";
  43. ContentValues reminderValues = new ContentValues();
  44. reminderValues.put("event_id",eventID);
  45. // Default value of the system. Minutes is a integer
  46. reminderValues.put("minutes",5);
  47. // Alert Methods: Default(0),Alert(1),Email(2),SMS(3)
  48. reminderValues.put("method",1);
  49. cr.insert(Uri.parse(reminderUriString),reminderValues); //Uri reminderUri =
  50. }
  51. if (isMailService) {
  52. String attendeuesesUriString = "content://com.android.calendar/attendees";
  53. /********* To add multiple attendees need to insert ContentValues multiple times ***********/
  54. ContentValues attendeesValues = new ContentValues();
  55. attendeesValues.put("event_id",eventID);
  56. // Attendees name
  57. attendeesValues.put("attendeeName","xxxxx");
  58. // Attendee email
  59. attendeesValues.put("attendeeEmail","yyyy@gmail.com");
  60. // Relationship_Attendee(1),Relationship_None(0),Organizer(2),Performer(3),Speaker(4)
  61. attendeesValues.put("attendeeRelationship",0);
  62. // None(0),Optional(1),required(2),Resource(3)
  63. attendeesValues.put("attendeeType",Accepted(1),Decline(2),Invited(3),Tentative(4)
  64. attendeesValues.put("attendeeStatus",0);
  65. cr.insert(Uri.parse(attendeuesesUriString),attendeesValues); //Uri attendeuesesUri =
  66. }
  67.  
  68. return eventID;
  69. }
  70.  
  71. }

我从我的项目中复制了这个类,所以欢迎您根据自己的喜好更改名称.
正如您在此处注意到的,您在插入后会收到id值.

如果您需要获得提醒我或与会者的会议ID – 您必须处理来自cr.insert的返回值…然后parce uri long id = Long.parseLong(uri.getLastPathSegment());

这是一个额外的信息:CalendarContract.EventsCalendar chapter

猜你在找的Android相关文章