无法从 Android 日历中删除提醒
我使用 caledarcontract api 以编程方式添加了一个日历事件并获得了一个 eventId。同样,我为此事件添加了提醒并保存了reminarId。现在我不想要此事件的提醒(或者我想关闭提醒),所以我尝试使用提醒 ID 删除提醒,但我无法删除。我也尝试使用 eventId 删除提醒,但它不起作用。
public int AddEventToCalendar(String calendarId, Entity entity) {
// TODO Auto-generated method stub
ContentValues event = new ContentValues();
event.put("calendar_id", calendarId);
event.put("title", entity.description);
event.put("dtstart", System.currentTimeMillis());
event.put("dtend", System.currentTimeMillis() + 3600*1000);
event.put("allDay", 0);
//status: 0~ tentative; 1~ confirmed; 2~ canceled
event.put("eventStatus", 1);
//0~ default; 1~ confidential; 2~ private; 3~ public
event.put("visibility", 0);
//0~ opaque, no timing conflict is allowed; 1~ transparency, allow overlap of scheduling
event.put("transparency", 0);
//0~ false; 1~ true
event.put("hasAlarm", 1);
Uri add_eventUri;
if (Build.VERSION.SDK_INT >= 8) {
add_eventUri = Uri.parse("content://com.android.calendar/events");
} else {
add_eventUri = Uri.parse("content://calendar/events");
}
Uri l_uri = context.getContentResolver().insert(add_eventUri, event);
if(l_uri != null)
{
long eventID = Long.parseLong(l_uri.getLastPathSegment());
return (int) eventID;
}
else
return 0;
}
public int AddReminderOnEvent(Entity entity)
{
if(entity.eventId != 0)
{
ContentValues reminderValues = new ContentValues();
reminderValues.put("event_id", entity.eventId);
reminderValues.put("method", 1);// will alert the user with a reminder notification
reminderValues.put("minutes", 0);// number of minutes before the start time of the event to fire a reminder
Uri reminder_eventUri;
if (Build.VERSION.SDK_INT >= 8) {
reminder_eventUri = Uri.parse("content://com.android.calendar/reminders");
} else {
reminder_eventUri = Uri.parse("content://calendar/reminders");
}
Uri r_uri = context.getContentResolver().insert(reminder_eventUri, reminderValues);
if(r_uri != null)
{
long reminderID = Long.parseLong(r_uri.getLastPathSegment());
return (int) reminderID;
// Toast.makeText(getApplicationContext(), "Event Created Successfully", Toast.LENGTH_LONG).show();
}
else
return 0;
}
else
{
return 0;
}
}
public boolean DeleteReminderOnTask(int eventId, int reminderId) {
// TODO Auto-generated method stub
Uri delete_reminderUri;
if (Build.VERSION.SDK_INT >= 8) {
delete_reminderUri = Uri.parse("content://com.android.calendar/reminders");
} else {
delete_reminderUri = Uri.parse("content://calendar/reminders");
}
delete_reminderUri = ContentUris.withAppendedId(delete_reminderUri, reminderId);
int rows = context.getContentResolver().delete(delete_reminderUri,null , null);
if(rows > 0)
return true;
else
return false;
}
每次执行此代码后,行都返回 0,这意味着没有行被更改。并且提醒会在适当的时间准确出现。如何从日历中删除提醒而不删除事件?
I have added a calendar event programatically using the caledarcontract api and obtained a eventId. Similarly i added a reminder for this event and saved the reminderId too. Now i dont want a reminder for this event(or i would like to turn off the reminder), so i am trying to delete the reminder using the reminderId but i am not able to delete. I tried to delete the reminder using the eventId too but its not working.
public int AddEventToCalendar(String calendarId, Entity entity) {
// TODO Auto-generated method stub
ContentValues event = new ContentValues();
event.put("calendar_id", calendarId);
event.put("title", entity.description);
event.put("dtstart", System.currentTimeMillis());
event.put("dtend", System.currentTimeMillis() + 3600*1000);
event.put("allDay", 0);
//status: 0~ tentative; 1~ confirmed; 2~ canceled
event.put("eventStatus", 1);
//0~ default; 1~ confidential; 2~ private; 3~ public
event.put("visibility", 0);
//0~ opaque, no timing conflict is allowed; 1~ transparency, allow overlap of scheduling
event.put("transparency", 0);
//0~ false; 1~ true
event.put("hasAlarm", 1);
Uri add_eventUri;
if (Build.VERSION.SDK_INT >= 8) {
add_eventUri = Uri.parse("content://com.android.calendar/events");
} else {
add_eventUri = Uri.parse("content://calendar/events");
}
Uri l_uri = context.getContentResolver().insert(add_eventUri, event);
if(l_uri != null)
{
long eventID = Long.parseLong(l_uri.getLastPathSegment());
return (int) eventID;
}
else
return 0;
}
public int AddReminderOnEvent(Entity entity)
{
if(entity.eventId != 0)
{
ContentValues reminderValues = new ContentValues();
reminderValues.put("event_id", entity.eventId);
reminderValues.put("method", 1);// will alert the user with a reminder notification
reminderValues.put("minutes", 0);// number of minutes before the start time of the event to fire a reminder
Uri reminder_eventUri;
if (Build.VERSION.SDK_INT >= 8) {
reminder_eventUri = Uri.parse("content://com.android.calendar/reminders");
} else {
reminder_eventUri = Uri.parse("content://calendar/reminders");
}
Uri r_uri = context.getContentResolver().insert(reminder_eventUri, reminderValues);
if(r_uri != null)
{
long reminderID = Long.parseLong(r_uri.getLastPathSegment());
return (int) reminderID;
// Toast.makeText(getApplicationContext(), "Event Created Successfully", Toast.LENGTH_LONG).show();
}
else
return 0;
}
else
{
return 0;
}
}
public boolean DeleteReminderOnTask(int eventId, int reminderId) {
// TODO Auto-generated method stub
Uri delete_reminderUri;
if (Build.VERSION.SDK_INT >= 8) {
delete_reminderUri = Uri.parse("content://com.android.calendar/reminders");
} else {
delete_reminderUri = Uri.parse("content://calendar/reminders");
}
delete_reminderUri = ContentUris.withAppendedId(delete_reminderUri, reminderId);
int rows = context.getContentResolver().delete(delete_reminderUri,null , null);
if(rows > 0)
return true;
else
return false;
}
After executing this code everytime the rows returns 0 meaning that no rows have been altered. And the reminder comes up exactly at the appropriate time. How to delete the reminder from the calendar without deleting the event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定失败时您运行的是哪个 SDK 版本,但这段代码(本质上与您的代码相同,减去版本检查)对我有用:
我通过查询获得了
reminderId
活动提醒:I'm not sure which SDK version you're running against when failing, but this code (which is essentially the same as yours, less the version check) works for me:
I got
reminderId
by querying the event's reminders:这可能不是唯一或最好的方法,但我所能想到的就是如何删除事件的所有提醒。我不知道有什么方法可以只删除一个提醒。
不幸的是,这会删除所有提醒,但我不确定 Android 14+ 或大多数日历提供商(例如 Exchange)是否真的支持多个提醒。 ICS 中的日历应用程序仅允许添加一个提醒(尽管显示“添加提醒”)。
如果我使用其他应用程序(例如商务日历)添加多个提醒,当我签入 Exchange 时,它只会显示一个提醒。它确实在日历应用程序中显示多个提醒,但仅在该设备上显示,而不是在其他设备上显示,因此多个提醒似乎仅是本地的。
This might not be the only or best way, but all I could figure out was how to remove all reminders for an event. I don't know of a way to remove just one reminder.
Unfortunately, this removes all reminders, but I'm not sure multiple reminders are really supported by Android 14+ or most calendar providers (e.g. Exchange). The calendar app in ICS only allows adding one reminder (despite saying "Add Reminders").
And if i use another application such as Business Calendar to add multiple reminders, when I check in Exchange, it only shows ones reminder. It does show multiple reminders in the calendar app but only on that device, not on other devices, so multiple reminders seem to be local only.