6

我正在尝试从我的代码更新我手机上的日历事件,但 context.getContentResolver().update 一直返回 0,当然,当我在日历应用程序中查看它时,没有对事件进行任何更改。

我正在使用 context.getContentResolver().query 获取事件 ID、开始时间等,并且我正在获取 431、4、233 等唯一数字,所以我假设我正在使用的事件 ID 是真实的。

我了解执行此操作的官方方法是通过 Google 的服务器而不是使用 update(),但对于我的实现而言,这样做没有意义(甚至一般来说,但我离题了)。

我是在做错什么,还是在尝试做 Android 根本不允许的事情?

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);

此处发布了一个相关问题,没有回复https://stackoverflow.com/questions/5636350/update-android-calendar-event

如果我能澄清任何事情,请告诉我;我非常感谢你们和洋娃娃能提供的任何意见!

4

3 回答 3

3

我尝试了很多,最后得到了解决方案(虽然不可靠)..但工作正常..

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;
    }
    }

最后我不确定它是否适用于所有设备。

于 2011-10-10T04:28:00.563 回答
2

好的,所以问题是我在获取事件和编辑它们之间使用了不同的 URI。我使用了此处的代码示例,并使用 URI“content://com.android.calendar/instances/when”来获取事件并将它们显示在屏幕上。当我进行更改时,我使用“content://com.android.calendar/events”按 id 进行编辑,如我上面的示例所示。

感谢您的回复,ntc,我发现两个 URI 之间的事件 id 不同,因此我无法根据每个给我的信息一致地编辑事件。我假设我得到的事件 id 是系统 id 并且是手机通用的。

我想我必须做一些测试,看看哪些硬件与这种方法不兼容。我正在使用 HTC Evo 进行测试,到目前为止一切顺利。

于 2011-10-12T04:59:05.063 回答
0

查询Instances表时,使用Instances.EVENT_ID获取要编辑的事件的标识符,而不是Instances._ID.

于 2013-12-30T21:01:00.593 回答