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

时间:2013-02-20 00:11:18

标签: 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);
    }

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

更新 问题仅在于我使用calendar_id=1进行插入和删除。其他calendar_id工作正常。

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

3 个答案:

答案 0 :(得分:17)

如果它不是URI问题,

您可以尝试加入选择字符串

AND (deleted != 1)

所以选择字符串变为,

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

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

p.s:尝试使用此工具 - http://www.cellobject.net/Tools/CellObjectSQLiteXMLBrowser.aspx可视化日历数据库。请检查db

中的“已删除”和“脏”列

答案 1 :(得分:1)

不同版本的android使用不同的URI路径来查找日历数据。例如,您可以为android 2.1和2.2使用以下常量:

private static final String URI_VERSION_2_1 = "content://calendar/events";
private static final String URI_VERSION_2_2 = "content://com.android.calendar/events";

在4.0中,首选不同的方法:

http://android-developers.blogspot.com/2011/10/ics-and-non-public-apis.html http://developer.android.com/reference/android/provider/CalendarContract.Events.html

Galaxy S3很有可能至少拥有Android 4.0。此更改可能会解释在S1上运行的代码,而不是S3。

答案 2 :(得分:0)

您也可以这样做:

private boolean isEventInCal(Context context, String id) {
    Uri event = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, Long.parseLong(id));

    Cursor cursor = context.getContentResolver().query(event, null, null, null, null);
    if (cursor != null && cursor.getCount() == 1) {
        //the event exists?
        if (cursor.moveToFirst() && !TextUtils.equals(cursor.getString(cursor.getColumnIndex("deleted")), "1")) {

            cursor.close();
            return true;
        } else {
            return false;
        }
    }
    return false;
}

适用于所有设备。