我正在尝试从我的代码更新手机上的日历事件,但是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

让我知道我是否可以澄清任何事情;非常感谢你们提供的任何输入!

最佳答案

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

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

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

10-07 19:15
查看更多