Guest User

Untitled

a guest
Apr 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. String eventUriString;
  2. if (Build.VERSION.SDK_INT > 7)
  3. eventUriString = "content://com.android.calendar/events";
  4. else eventUriString = "content://calendar/events";
  5.  
  6. ContentValues eventValues = new ContentValues();
  7.  
  8. eventValues.put("calendar_id", 1);
  9. eventValues.put("title", "title");
  10. eventValues.put("description", desc);
  11. eventValues.put("eventLocation", "");
  12.  
  13. long endDate = startDate + 1000 * 60 * 60;
  14.  
  15. eventValues.put("dtstart", MyClass.getDate());
  16. eventValues.put("dtend", endDate);
  17.  
  18. eventValues.put("eventStatus", 0);
  19. eventValues.put("visibility", 2);
  20. eventValues.put("transparency", 0);
  21.  
  22. eventValues.put("hasAlarm", 0);
  23.  
  24. Uri eventUri = context.getContentResolver().insert(Uri.parse(eventUriString), eventValues);
  25. long eventID = Long.parseLong(eventUri.getLastPathSegment());
  26.  
  27. String calendarUriBase = null;
  28. long id = MyEvents.getID(p); //something ID from my another class
  29. Uri calendars = Uri.parse("content://calendar/calendars");
  30. Cursor managedCursor = null;
  31. try {
  32. managedCursor = managedQuery(calendars, null, null, null, null);
  33. } catch (Exception e) {
  34. // eat
  35. }
  36.  
  37. if (managedCursor != null) {
  38. calendarUriBase = "content://calendar/";
  39. } else {
  40. calendars = Uri.parse("content://com.android.calendar/calendars");
  41. try {
  42. managedCursor = managedQuery(calendars, null, null, null, null);
  43. } catch (Exception e) {
  44. // eat
  45. }
  46.  
  47. if (managedCursor != null) {
  48. calendarUriBase = "content://com.android.calendar/";
  49. }
  50.  
  51. }
  52.  
  53. ContentValues event = new ContentValues();
  54.  
  55. event.put("title", "new Title");
  56.  
  57. Uri eventsUri = Uri.parse(calendarUriBase+"events");
  58. Uri eventUri = ContentUris.withAppendedId(eventsUri, id);
  59.  
  60. getContentResolver().update(eventUri, event, null, null);
  61.  
  62. if (Build.VERSION.SDK_INT >= 14) {
  63. saveCalendarEventICS();
  64. }
  65. else {
  66. int cal_id = getCalendar_ID();
  67. if(cal_id != 0){
  68. saveCalendarEvent(cal_id);
  69. }
  70. }
  71.  
  72. private int getCalendar_ID() {
  73. // TODO Auto-generated method stub
  74. int calendar_id = 0;
  75. String[] projection = new String[] { "_id", "name" };
  76. String selection = "selected=1";
  77. String path = "calendars";
  78. Cursor calendarCursor = getCalendarCursor(projection, selection, path);
  79.  
  80. if (calendarCursor != null && calendarCursor.moveToFirst()) {
  81. int nameColumn = calendarCursor.getColumnIndex("name");
  82. int idColumn = calendarCursor.getColumnIndex("_id");
  83. do {
  84. String calName = calendarCursor.getString(nameColumn);
  85. String calId = calendarCursor.getString(idColumn);
  86. if (calName != null /*&& calName.contains("Test")*/) {
  87. calendar_id = Integer.parseInt(calId);
  88. }
  89. } while (calendarCursor.moveToNext());
  90. }
  91. return calendar_id;
  92. }
  93.  
  94. private void saveCalendarEventICS() {
  95.  
  96. Intent intent = new Intent(Intent.ACTION_INSERT)
  97. .setType("vnd.android.cursor.item/event")
  98. .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, frsttime)
  99. .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, sncdtime)
  100. .putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY , false) // just included for completeness
  101. .putExtra(Events.TITLE, vl_hldr[0])
  102. .putExtra(Events.DESCRIPTION, vl_hldr[2])
  103. .putExtra(Events.EVENT_LOCATION, vl_hldr[1])
  104. .putExtra(Events.RRULE, "FREQ=DAILY;COUNT=10")
  105. .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
  106. .putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE)
  107. .putExtra(Events.ALLOWED_REMINDERS, "METHOD_DEFAULT")
  108. .putExtra(Intent.EXTRA_EMAIL, "");
  109. startActivity(intent);
  110. }
  111.  
  112.  
  113. private void saveCalendarEvent(int calid) {
  114. // TODO Auto-generated method stub
  115.  
  116. //Create the event here -----------
  117.  
  118.  
  119. Uri newEvent;
  120. if (Build.VERSION.SDK_INT >= 8) {
  121. //newEvent = Uri.parse("content://com.android.calendar/events");
  122. newEvent = ctx.getContentResolver().insert(Uri.parse("content://com.android.calendar/events"), event);
  123.  
  124. if (newEvent != null) {
  125. long id = Long.parseLong( newEvent.getLastPathSegment() );
  126. ContentValues values = new ContentValues();
  127. values.put( "event_id", id );
  128. values.put( "method", 1 );
  129. values.put( "minutes", 15 ); // 15 minuti
  130.  
  131. getContentResolver().insert( Uri.parse( "content://com.android.calendar/reminders" ), values );
  132. }
  133. }
  134. else {
  135. newEvent = ctx.getContentResolver().insert(Uri.parse("content://calendar/events"), event);
  136.  
  137. if (newEvent != null) {
  138. long id = Long.parseLong( newEvent.getLastPathSegment() );
  139. ContentValues values = new ContentValues();
  140. values.put( "event_id", id );
  141. values.put( "method", 1 );
  142. values.put( "minutes", 15 ); // 15 minuti
  143.  
  144. getContentResolver().insert( Uri.parse( "content://calendar/reminders" ), values );
  145. }
  146. }
  147.  
  148. }catch(Exception ee){}
  149.  
  150. }
Add Comment
Please, Sign In to add comment