无法通过代码更新手机日历上的活动

发布于 2024-12-07 20:15:40 字数 1154 浏览 3 评论 0原文

我试图通过我的代码更新手机上的日历事件,但 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白馒头 2024-12-14 20:15:40

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

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

最后我不确定它是否适用于每个设备。

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.

风轻花落早 2024-12-14 20:15:40

好吧,问题是我在获取事件和编辑事件之间使用了不同的 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.

疯狂的代价 2024-12-14 20:15:40

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文