我正在尝试让我的应用程序向用户的日历添加提醒。代码在添加之前搜索title
andstart date
以检查事件是否已存在于日历中(以免重复)。我的问题是:如果我手动从日历中删除事件(使用日历),事件会从日历中消失(我正在查看我的所有日历,但在日历应用程序中看不到它),但不会从数据库中消失。我不明白的另一件事是我试图以编程方式删除事件,但它们仍然没有被删除。
这是我的代码片段:
Cursor cur = null;
ContentResolver cr = getContentResolver();
String calUriString = "content://com.android.calendar/events";
Uri cal=Uri.parse(calUriString);
String[] EVENT_PROJECTION=new String[]{"calendar_id","title","dtstart","_id"};
String selection = "((" + "calendar_id" + " = 1) AND ("
+ "title" + " LIKE '"+name+"') AND ("
+ "dtstart" + " = "+String.valueOf(c.getTimeInMillis())+"))";
cur = cr.query(cal, EVENT_PROJECTION, selection, null, null);
boolean found=false;
if(cur!=null)
if(cur.moveToFirst()){
DatabaseUtils.dumpCursor(cur);/*Just to view my results (which always include the deleted events)*/
do{
if(cur.getString(1).equals(name)){
/*I use this part to try to remove the events manually*/
Uri eventUri =ContentUris.withAppendedId(cal, Long.valueOf(cur.getString(3)));
String reminderUriString = "content://com.android.calendar/reminders";
Uri remUri =Uri.parse(reminderUriString);
cr.delete(remUri, "event_id="+cur.getString(3), null);
cr.delete(eventUri, null, null);
/*It is not working also*/
Toast.makeText(getApplicationContext(), "Event already exists in Calendar", Toast.LENGTH_LONG).show();
found=true;
//break;
}
}while(cur.moveToNext());
}
cur.close();
if(found){
/*Event is found even if I remove it from the Calendar manually and even if I remove it programmatically using cr.delete()*/
}
else{
values.put("calendar_id",1); /*I am using the same Calendar that I query, or is this wrong*/
values.put("title", name);
/*More values are added*/
Uri calendarUri = cr.insert(cal, values);
long eventID = Long.parseLong(calendarUri.getLastPathSegment());
String reminderUriString = "content://com.android.calendar/reminders";
ContentValues reminderValues = new ContentValues();
reminderValues.put("event_id", eventID);
reminderValues.put("minutes", 5);
reminderValues.put("method", 1);
Uri reminderUri = getApplicationContext().getContentResolver().insert(Uri.parse(reminderUriString), reminderValues);
}
为什么从日历应用程序中删除事件后仍然存在,为什么我什至无法以编程方式将其删除?
更新
问题只有当我calendar_id=1
用于插入和删除时。其他calendar_id
的工作正常。
更新 2 我在三星 Galaxy S1 上测试了代码,它运行良好。这似乎是三星 Galaxy S3 的问题(我有问题,我认为这是 S-planner 应用程序中的错误)