我的应用程序中有一个事件列表。侧面的按钮允许用户将事件日期和时间添加到他/她的日历中。我使用日历意图将用户重定向到android日历,其中对应的日期和时间。现在,在用户将事件添加到日历后,我想禁用与他/她已经添加的事件相对应的“添加事件”按钮(这样用户就可以避免再次添加相同的事件)。我该怎么做呢?我已经尝试过android 4.0的新日历API,但我无法实现我想要的东西。
基本上,我想要的是避免在用户日历中重复输入相同的事件。
任何帮助都将不胜感激。
发布于 2012-11-20 19:00:17
您应该测试此事件的实例是否存在。请参阅documentation of the Android's CalendarContract.Instances class。
特别是在这种情况下,second query method应该会有所帮助。
这个例子是我在blog post about the CalendarContract provider上发布的一些代码--根据你的需要稍微修改一下:
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"!
并且不要忘记包括必要的权限。
发布于 2019-07-12 04:42:31
不建议在UI线程上运行Instances.query,但可以通过确保最小化开始和结束持续时间来高效地运行。
搜索字符串将搜索所有值,而不仅仅是title,因此需要添加一个循环来检查确切的字段值。
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;
}
发布于 2013-08-07 12:42:59
我使用了以下方法检查它,我正在通过event_id检查它是否在日历中...
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;
}
https://stackoverflow.com/questions/13466963
复制相似问题