检查该事件是否存在,然后将其添加到Android日历中 [英] Check if the event exists before adding it to the Android calender

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

问题描述

我有一个在我的应用程序的事件列表。侧面的按钮允许用户向他/她的日历添加事件日期和时间。我使用日历意图将用户重定向到Android日历,相应的日期和时间。现在,在用户将事件添加到其日历之后,我想禁用与他/她已经添加的事件相对应的添加事件按钮(因此用户避免再次添加相同的事件)。我如何做到这一点?我已经通过新的日历API为Android 4.0,但我不能够实现我想要的。

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.

推荐答案

如果此事件的实例存在,您应该测试。请参阅 Android的CalendarContract.Instances类的文档。

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.

这些示例是一些代码,发布在我的博客上发布关于CalendarContract提供程序 - 根据您的需求稍作修改:

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.

最后一个参数应包含您添加到日历的活动的标题。保持报价 - 否则Android会寻找您的或事件或标题!

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日历中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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