首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将事件添加到Android日历之前,请检查事件是否存在

将事件添加到Android日历之前,请检查事件是否存在
EN

Stack Overflow用户
提问于 2012-11-20 13:12:30
回答 4查看 6.9K关注 0票数 3

我的应用程序中有一个事件列表。侧面的按钮允许用户将事件日期和时间添加到他/她的日历中。我使用日历意图将用户重定向到android日历,其中对应的日期和时间。现在,在用户将事件添加到日历后,我想禁用与他/她已经添加的事件相对应的“添加事件”按钮(这样用户就可以避免再次添加相同的事件)。我该怎么做呢?我已经尝试过android 4.0的新日历API,但我无法实现我想要的东西。

基本上,我想要的是避免在用户日历中重复输入相同的事件。

任何帮助都将不胜感激。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-11-20 19:00:17

您应该测试此事件的实例是否存在。请参阅documentation of the Android's CalendarContract.Instances class

特别是在这种情况下,second query method应该会有所帮助。

这个例子是我在blog post about the CalendarContract provider上发布的一些代码--根据你的需要稍微修改一下:

代码语言:javascript
复制
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
}

请注意:从纪元开始,时间总是以UTC毫秒为单位。因此,您可能需要根据用户的时区进行调整。

最后一个参数应该包含已添加到日历中的事件的标题。保留引号-否则Android会查找"your“、"event”或"title"!

并且不要忘记包括必要的权限。

票数 7
EN

Stack Overflow用户

发布于 2019-07-12 04:42:31

不建议在UI线程上运行Instances.query,但可以通过确保最小化开始和结束持续时间来高效地运行。

搜索字符串将搜索所有值,而不仅仅是title,因此需要添加一个循环来检查确切的字段值。

代码语言:javascript
复制
public boolean eventExistsOnCalendar(String eventTitle, long startTimeMs, long endTimeMs) {
  if (eventTitle == null || "".equals(eventTitle)) {
    return false;
  }
  if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
    return false;
  }
  // If no end time, use start + 1 hour or = 1 day. Query is slow if searching a huge time range
  if (endTimeMs <= 0) {
    endTimeMs = startTimeMs + 1000 * 60 * 60; // + 1 hour
  }

  final ContentResolver resolver = mContext.getContentResolver();
  final String[] duplicateProjection = {CalendarContract.Events.TITLE}; // Can change to whatever unique param you are searching for
  Cursor cursor =
      CalendarContract.Instances.query(
          resolver,
          duplicateProjection,
          startTimeMs,
          endTimeMs,
          '"' + eventTitle + '"');

  if (cursor == null) {
    return false;
  }
  if (cursor.getCount() == 0) {
    cursor.close();
    return false;
  }

  while (cursor.moveToNext()) {
    String title = cursor.getString(0);
    if (eventTitle.equals(title)) {
      cursor.close();
      return true;
    }
  }

  cursor.close();
  return false;
}
票数 1
EN

Stack Overflow用户

发布于 2013-08-07 12:42:59

我使用了以下方法检查它,我正在通过event_id检查它是否在日历中...

代码语言:javascript
复制
public boolean isEventInCal(Context context, String cal_meeting_id) {

     Cursor cursor = context.getContentResolver().query(
     Uri.parse("content://com.android.calendar/events"),
       new String[] { "_id" }, " _id = ? ",
       new String[] { cal_meeting_id }, null);

           if (cursor.moveToFirst()) {
                   //Yes Event Exist...
      return true;
     }
     return false;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13466963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档