我想让我的应用程序添加提醒到用户的日历。该代码搜索title
和start 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规划应用程序中的错误)的问题
如果你重新启动你的应用程序,被删除的事件是否仍然出现在它中? – NathanTempelman 2013-02-22 22:41:38
是的,即使我重新启动手机,它们也不会出现在日历应用程序中,但仍会出现在ContentResolver中。查询() – 2013-02-23 13:43:48
@Mohamed_AbdAllah我不能肯定地说,如果是这样的话,但类似的问题可以看出与流派和音乐的查询。你需要弄清楚它是否有bug =/ – Warpzit 2013-03-06 17:59:28