无法通过代码更新手机日历上的活动
我试图通过我的代码更新手机上的日历事件,但 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
如果我可以澄清任何事情,请告诉我;我真的很感激你们和娃娃可以提供的任何意见!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我尝试了很多,最终得到了解决方案(虽然不可靠)..但工作正常..
最后我不确定它是否适用于每个设备。
i had tried a lot and finally ended up with solution (Unreliable though).. but works fine..
and finally i'm not sure if it works with every device.
好吧,问题是我在获取事件和编辑事件之间使用了不同的 URI。我使用了此处 并使用 URI“content://com.android.calendar/instances/when”来获取事件并将其显示在屏幕上。当我进行更改时,我使用“content://com.android.calendar/events”按 id 进行编辑,如上面的示例所示。
感谢您的回复,ntc,我发现两个 URI 之间的事件 id 不同,因此我无法根据每个事件向我提供的信息来编辑事件。我假设我得到的事件 ID 是系统 ID,并且是手机通用的。
我想我必须做一些测试,看看哪些硬件与此方法不兼容。我正在使用 HTC Evo 进行测试,到目前为止一切顺利。
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.
查询
Instances
表时,使用Instances.EVENT_ID
获取要编辑的事件的标识符,而不是Instances._ID
。When querying the
Instances
table, useInstances.EVENT_ID
to get the identifier for the event you want to edit, instead ofInstances._ID
.