gpt4 book ai didi

android - 无法通过代码更新手机日历上的事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:01:55 24 4
gpt4 key购买 nike

我试图通过我的代码更新手机上的日历事件,但 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;
}
}

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

关于android - 无法通过代码更新手机日历上的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7656343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com