问题描述
我一直在浏览有关如何从我的 Android 2.3 手机日历中仅删除一个重复事件实例的各种帖子,但我一直未能找到完美的答案.到目前为止,我遇到的最好的是:
I've been looking through the various posts on how to delete just one instance of a recurring event from my Android 2.3 Phone's calendar, but I haven't been able to find the perfect answer. So far, the best I've come across is :
Uri eventsUri = Uri.parse("content://com.android.calendar/events");
Uri deleteEventUri = Uri.withAppendedPath(eventsUri, String.valueOf(id2));
DeleteEventActivity.context.getContentResolver().delete(deleteEventUri, null, null);
其中 id2 是我要删除的事件的 ID.我遇到的问题是我只想删除重复事件的一个实例,但是此代码会删除所有事件.有没有办法只删除一个实例?谢谢.
Where id2 is the id of the event I want to delete. The problem I have is that I only want to delete one instance of a recurring event, but this code deletes all occurrences. Is there a way to delete only one instance? Thanks.
推荐答案
如果还有人为此苦苦挣扎,我终于破解了!
If anyone else is still struggling with this, I've finally cracked it!
事实证明,您并没有删除或修改任何记录,您实际上所做的是插入一个称为异常的新事件.我花了几个月的时间在任何地方找到这些信息,即使找到了,又花了几个月的时间来弄清楚需要在异常中添加什么才能使其正常工作,所以我是这样做的.
It turns out that you do not delete or amend any records, what you actually do is insert a new event called an exception. It took me a few months to find this information anywhere, and even having found it, another few months to work out exactly what needed to be put into the exception to make it work, so here is how I did it.
首先,查询您要取消的事件的实际实例.您需要查询表 CalendarContract.Instances 以获取 CalendarContract.Instances.TITLE 的值,CalendarContract.Instances.BEGIN 和 CalendarContract.Instances.EVENT_ID.我在代码中执行此操作的方式并不真正适合此答案的上下文,因此希望您能够自己弄清楚如何做到这一点.将这些值存储为:
Firstly, query the actual instance of the event that you want to cancel one occurrence of. You need to query the table CalendarContract.Instances to get the values of CalendarContract.Instances.TITLE,CalendarContract.Instances.BEGIN and CalendarContract.Instances.EVENT_ID. The way I do this in my code doesn't really fit in with the context of this answer, so hopefully you will be able to work out how to do that yourself. Store these values as:
最终字符串标题 = eventCursor.getString(0);最后的 long beginl=eventCursor.getLong(1);final int id = eventCursor.getInt(2);
那么我们需要设置一个新的事件如下:
Then we need to set up a new event as follows :
final Context context = getApplicationContext();
final ContentResolver contentResolver = context.getContentResolver();
final Uri.Builder builder = Uri.parse(
"content://com.android.calendar/events").buildUpon();
final Cursor eventCursor = contentResolver.query(builder.build(),
new String[]
{Events.CALENDAR_TIME_ZONE,Events.ALL_DAY,Events.CALENDAR_ID,Events._SYNC_ID, Events.OWNER_ACCOUNT },
"_id=" + id, null, null);
while (eventCursor.moveToNext()) {
final String timezone=eventCursor.getString(0);
final String allday=eventCursor.getString(1);
final long calID=eventCursor.getLong(2);
final String mSyncId=eventCursor.getString(3);
final String account=eventCursor.getString(4);
ContentValues values = new ContentValues();
values.put(Events.TITLE, title);
values.put(Events.EVENT_TIMEZONE, timezone);
values.put(Events.ORIGINAL_SYNC_ID, mSyncId);//not 100% sure about this,may also need a date?
values.put(Events.HAS_ALARM, "0");
values.put(Events.HAS_ATTENDEE_DATA,"0");
values.put(Events.ALL_DAY, allday);
values.put(Events.DTSTART, beginl+3600000);
values.put(Events.ORIGINAL_INSTANCE_TIME, beginl+3600000);
values.put(Events.STATUS, Events.STATUS_CANCELED);
Uri uri = Uri.withAppendedPath(Events.CONTENT_EXCEPTION_URI,
String.valueOf(id));
uri = asSyncAdapter(uri, account, CTS_TEST_TYPE);
Uri resultUri = context.getContentResolver().insert(uri, values);
try {
int eventID = Integer.parseInt(resultUri.getLastPathSegment());
int debug=eventID;
} catch (Exception e) {
int debug=0;
}
}
static Uri asSyncAdapter(Uri uri, String account, String accountType) {
return uri.buildUpon()
.appendQueryParameter
(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true")
.appendQueryParameter(Calendars.ACCOUNT_NAME, account)
.appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build();
}
希望这可行,我已尝试删除我的代码中不相关的部分.您会注意到我必须将 3600000 添加到 beginl 值(以毫秒为单位的 1 小时).我认为这是因为我们目前处于 BST 中,一旦时钟发生变化,此代码将无法工作,但当时我会担心的!
Hope this works, I've tried to cut out the parts of my code which are not relevant. You'll note I am having to add 3600000 to the beginl value (1 hour in milliseconds). I assume that is because we are in BST at the moment and this code will not work once the clocks change, but I'll worry about that at the time!
最后,我使用了一个名为 Content Provider Helper 的应用程序来帮助我解决这个问题.您可以使用它查询您的内容提供程序表.我会尝试使用我的代码设置异常,然后使用我的手机日历应用删除实例,并比较两条记录.
Finally, I used an app called Content Provider Helper to help me find this out. You can use it query your content provider tables. I would try setting up an exception using my code, then use my phones calendar app to delete the instance, and compare the two records.
这篇关于从我的 Android 日历中仅删除一个重复事件的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!