Unable to update phone calendar event from code

I am trying to update a calendar event on my phone from my code, but context.getContentResolver (). update continues to return 0, and of course, no changes made to the event when I look at it in the application calendar.

I get the event id, start time, etc. with the request context.getContentResolver (). I get unique numbers, for example 431, 4, 233, etc., so I assume that the event IDs, m, are real.

I understand that the official way to do this is to go through the Google servers instead of using update (), but for my implementation it makes no sense to do it this way (or even in general, but I'm distracted).

Am I doing something wrong, or am I trying to do something that Android just won't allow?

Uri updateEventUri = ContentUris.withAppendedId(Uri.parse("content://com.android.calendar/events"), id); ContentValues cv = new ContentValues(); begin.set(Calendar.HOUR_OF_DAY, arg0.getCurrentHour()); //begin is a java.util.Calendar object begin.set(Calendar.MINUTE, arg0.getCurrentMinute()); //cv.put("_id", id); //cv.put("title", "yeahyeahyeah!"); cv.put("dtstart", begin.getTimeInMillis()); int updatedrowcount = context.getContentResolver().update(updateEventUri, cv, null, null); System.out.println("updated "+updatedrowcount+" rows with id "+id); 

A related question was posted here unanswered https://stackoverflow.com/questions/5636350/update-android-calendar-event

Let me know if I can clarify anything; I would really appreciate any materials that the guys and dolls could provide you!

+6
source share
3 answers

I tried a lot and finally ended up with a solution (unreliable though) .. but works fine.

 public static boolean updateCalendar(Context context,String cal_Id,String eventId) { try{ Uri CALENDAR_URI = Uri.parse(CAL_URI+"events"); Cursor c = context.getContentResolver().query(CALENDAR_URI, null, null, null, null); String[] s = c.getColumnNames(); if (c.moveToFirst()) { while (c.moveToNext()) { String _id = c.getString(c.getColumnIndex("_id")); String CalId = c.getString(c.getColumnIndex("calendar_id")); if ((_id==null) && (CalId == null)) { return false; } else { if (_id.equals(eventId) && CalId.equals(cal_Id)) { Uri uri = ContentUris.withAppendedId(CALENDAR_URI, Integer.parseInt(_id)); context.getContentResolver().update(uri, null, null, null);// need to give your data here return true; } } } } } finally { return true; } } 

and finally, I'm not sure that it works with every device.

+3
source

So the problem was that I used different URIs between fetching events and editing them. I used the sample code from here and used the URI "content: //com.android.calendar/instances/when" to retrieve events and display them on the screen. When I made the changes, I used "content: //com.android.calendar/events" to edit by id, as in my example above.

What I found, thanks to your answer, ntc, was that the identifiers for the events between the two URIs were different, and therefore I could not edit the events sequentially with the information I was given. I assumed that the event identifiers I received were system identifiers and universal for the phone.

I think I will need to do some testing and see which equipment is not compatible with this method. I use HTC Evo for testing and it is still so good.

+2
source

When querying the Instances table, use Instances.EVENT_ID to get the identifier for the event you want to change, instead of Instances._ID .

0
source

Source: https://habr.com/ru/post/898642/


All Articles