Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't update event on phone's calendar from code

I'm attempting to update a calendar's event on my phone from my code, but context.getContentResolver().update keeps returning 0, and of course there are no changes made to the event when I look at it in the Calendar app.

I'm getting the event ID, start time, etc with context.getContentResolver().query, and I'm getting unique numbers like 431, 4, 233, etc, so I'm presuming the event IDs I'm using are real.

I understand the official way to do this is to go through Google's servers instead of using update(), but for my implementation it doesn't make sense to do it that way (or even in general, but I digress).

Am I doing something wrong, or am I trying to do something that Android simply isn't going to 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 with no replies https://stackoverflow.com/questions/5636350/update-android-calendar-event

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

like image 949
atraudes Avatar asked Oct 05 '11 03:10

atraudes


People also ask

How do you update a calendar event?

Double-click the meeting on your calendar to open it. Change the location, start and end times, attendees, message, or other options. On the Organizer Meeting or Meeting tab, click Send Update.


2 Answers

i had tried a lot and finally ended up with 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 if it works with every device.

like image 189
ngesh Avatar answered Sep 28 '22 14:09

ngesh


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

What I found, thanks to your response, ntc, was that the ids for events between the two URIs were different, and therefore I couldn't edit the events consistently with the information each was giving me. I was presuming the event ids I was getting were system ids and universal to the phone.

I guess I'll have to do some testing and see what hardware isn't compatible with this method. I am using an HTC Evo for testing and so far so good.

like image 38
atraudes Avatar answered Sep 28 '22 13:09

atraudes