删除事件从日历不会被删除日历、事件

由网友(怪我入戏太深ゝ)分享简介:我试图让我的应用程序添加提醒用户的日历。为标题和开始日期的code搜索来检查,如果事件已经存在于日历前加入它(以免有重复)。我的问题是:如果我从日历手动删除该事件(使用日历),该事件从日历(我查看我所有的日历,并不能看到它的日历应用程序),但不会从数据库中消失。另一件事我不明白的是,我试图以编程方式删除的事件,但他们仍...

我试图让我的应用程序添加提醒用户的日历。为标题开始日期的code搜索来检查,如果事件已经存在于日历前加入它(以免有重复)。我的问题是:如果我从日历手动删除该事件(使用日历),该事件从日历(我查看我所有的日历,并不能看到它的日历应用程序),但不会从数据库中消失。另一件事我不明白的是,我试图以编程方式删除的事件,但他们仍然不会被删除。

I am trying to make my App add reminders to the user's Calendar. The code searches for the title and start date to check if the event already exists in the Calendar before adding it (so as not to have duplicates). My problem is that: if I remove the event from the Calendar manually (using the Calendar), the event disappears from the Calendar (I am viewing all my Calendars and can't see it in the Calendar Application) but not from the DataBase. Another thing I don't understand is that I tried to remove the events programmatically, but still they are not removed.

下面是我的code片断:

Here is my code snippet:

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);
    }

为什么事件依然present从日历应用程序中取出后为什么我不能删除它,甚至编程?

Why are the events still present after removing them from the calendar Application and why am I not able to remove it even programmatically?

更新 问题是,只有当我使用 CALENDAR_ID = 1 插入和删除。其他 CALENDAR_ID 的工作就好了。

Update The problem is only if I use calendar_id=1 for inserting and deleting. Other calendar_ids work fine.

更新2 我测试了三星Galaxy S1的code,这是工作的罚款。这似乎是在三星Galaxy S3的问题(在那里我有我的问题,我认为这是在S-规划者应用程序中的错误)

Update 2 I tested the code on Samsung Galaxy S1 and it is working fine. It seems to be a problem in Samsung Galaxy S3 (where I have my problem, I think it is a bug in the S-planner App)

推荐答案

如果它不是一个URI问题,

If its not a URI problem,

您可以尝试在选择字符串包含

Can you try to include in the selection string

AND (deleted != 1)

所以选择字符串变,

so the selection string becomes,

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

偶尔,CalendarDB取某时的事件被去除。但是,删除列将被标记为1,表示他们将尽快删除。究其原因,延迟也许日历正在等待同步

Occasionally, the CalendarDB takes sometime for the events to be removed. But the 'deleted' COLUMN will be marked as '1' indicating that they will be deleted soon. The reason for the delay maybe the calendar is waiting to be synced

P.S:尝试使用这个工具 - http://www.cellobject.net/Tools/CellObjectSQLiteXMLBrowser.aspx以可视化的日历数据库。请在数据库检查的删除和脏列

p.s: Try using this tool -- http://www.cellobject.net/Tools/CellObjectSQLiteXMLBrowser.aspx to visualize the Calendar DB. Please check for the 'deleted' and 'dirty' columns in the db

阅读全文

相关推荐

最新文章