本文介绍了将事件添加到Android日历之前,请检查事件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个事件列表。侧面的按钮可让用户将事件日期和时间添加到他/她的日历。我使用日历意图将用户重定向到相应日期和时间的android日历。现在,在用户将事件添加到他的日历之后,我想禁用与他/她已添加的事件相对应的添加事件按钮(因此用户可以避免再次添加相同的事件)。我该怎么做?我已经通过了Android 4.0的新的日历API,但我无法实现我想要的。



基本上我想要的是避免重复输入相同的事件

解决方案

如果存在此事件的实例,则应测试。请参阅的文档。



特别是在这种情况下应该是有帮助的。



此示例是一些代码,我发布在我的 - 根据您的需要略有更改:

  long begin = //以毫秒为单位的开始时间
long end = //以毫秒为单位的结束时间
String [] proj =
new String [] {
Instances._ID,
Instances.BEGIN,
Instances.END,
Instances.EVENT_ID};
Cursor cursor =
Instances.query(getContentResolver(),proj,begin,end,\你的事件title \);
if(cursor.getCount()> 0){
//处理冲突
}

$ b请注意:从时代开始,始终以UTC毫秒为单位。因此,您可能需要根据用户的时区进行调整。



最后一个参数应包含已添加到日历中的事件的标题。保持引号 - 否则Android会查找您的或事件或标题!



不要忘记包含必要的权限。


I have a list of events in my app. A button on the side lets the user add the event date and time to his/her calender. I use a calender intent to redirect the user to the android calender which the corresponding date and time. Now after the user adds the event to his calender, I would like to disable the 'add event' button which corresponds to the events he/she had already added(so the user avoid adding the same event again). How can I do this? I have gone through the new calender API for android 4.0 but I wasnt able to achieve what I wanted.

Basically what I want is to avoid repeated entries for the same event in the users calender.

Any help would be appreciated.

解决方案

You should test, if an instance for this event exists. See the documentation of the Android's CalendarContract.Instances class.

Especially the second query method should be helpful in this case.

This examples is some code, I posted on my blog post about the CalendarContract provider - slightly altered for your needs:

long begin = // starting time in milliseconds
long end = // ending time in milliseconds
String[] proj =
      new String[]{
            Instances._ID,
            Instances.BEGIN,
            Instances.END,
            Instances.EVENT_ID};
Cursor cursor =
      Instances.query(getContentResolver(), proj, begin, end, "\"Your event title\"");
if (cursor.getCount() > 0) {
   // deal with conflict
}

Be aware: The time is always in UTC millis since the epoch. So you might have to adjust given the user's timezone.

And the last parameter should contain the title of the event you have added to the calendar. Keep the quotes - otherwise Android looks for "your" or "event" or "title"!

And do not forget to include the necessary permissions.

这篇关于将事件添加到Android日历之前,请检查事件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 01:58
查看更多