只从我的Android日历中删除一个周期性活动的一个实例 [英] Delete only one instance of a recurring event from my Android calendar

查看:465
本文介绍了只从我的Android日历中删除一个周期性活动的一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览关于如何从我的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:

final String title = eventCursor.getString(0);
final long beginl = eventCursor.getLong(1);
final int id = eventCursor.getInt(2);

然后我们需要设置一个新事件如下: / p>

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

希望这项工作,我的代码的部分是不相关的。你会注意到,我必须添加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日历中删除一个周期性活动的一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆