从日历中删除未删除的事件

Moh*_*lah 26 android calendar android-calendar

我正在尝试让我的应用程序向用户的日历添加提醒.代码在添加之前搜索titlestart 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);
    }
Run Code Online (Sandbox Code Playgroud)

为什么从日历应用程序中删除事件后仍然存在事件,为什么我甚至无法以编程方式删除它?

更新 问题仅在我calendar_id=1用于插入和删除时.其他calendar_id的工作正常.

更新2 我测试了三星Galaxy S1上的代码,它运行正常.这似乎是三星Galaxy S3中的问题(我有我的问题,我认为这是S-planner应用程序中的一个错误)

小智 17

如果它不是URI问题,

你能尝试包含在选择字符串中吗?

AND (deleted != 1)
Run Code Online (Sandbox Code Playgroud)

所以选择字符串变成,

String selection = "((" + "calendar_id" + " = 1) AND ("
    + "title" + " LIKE '"+name+"') AND ("
    + "dtstart" + " = "+String.valueOf(c.getTimeInMillis())+") AND ( deleted != 1 ) )";
Run Code Online (Sandbox Code Playgroud)

有时,CalendarDB需要一些时间才能删除事件.但"已删除"的列将标记为"1",表示它们很快就会被删除.延迟的原因可能是日历等待同步

ps:尝试使用此工具 - http://www.cellobject.net/Tools/CellObjectSQLiteXMLBrowser.aspx来可视化Calendar DB.请检查数据库中的"已删除"和"脏"列