2013-02-20 62 views
25

我想让我的应用程序添加提醒到用户的日历。该代码搜索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规划应用程序中的错误)的问题

+0

如果你重新启动你的应用程序,被删除的事件是否仍然出现在它中? – NathanTempelman 2013-02-22 22:41:38

+0

是的,即使我重新启动手机,它们也不会出现在日历应用程序中,但仍会出现在ContentResolver中。查询() – 2013-02-23 13:43:48

+0

@Mohamed_AbdAllah我不能肯定地说,如果是这样的话,但类似的问题可以看出与流派和音乐的查询。你需要弄清楚它是否有bug =/ – Warpzit 2013-03-06 17:59:28

回答

17

如果它不是一个URI问题,

你可以尝试选择字符串

AND (deleted != 1) 

所以选择字符串变成包括

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

偶尔CalendarDB需要一段时间为要删除的事件。但'已删除'COLUMN将被标记为'1',表示它们将很快被删除。延迟的原因可能是日历正在等待同步

p.s:尝试使用此工具 - http://www.cellobject.net/Tools/CellObjectSQLiteXMLBrowser.aspx可视化日历数据库。请分贝检查“被删除”和“脏”列

+1

Galaxy S3上的S-planner应用程序中仍然存在一个错误,因为删除的事件不会随着时间的推移而被删除。您的代码用于从查询结果中过滤已删除的事件,但我切换手机的On&Off并等待了很长时间,事件仍在ContentProvider数据库中(尽管列已删除= 1)并且从未删除。 – 2013-04-14 20:57:56

+0

在删除活动的日历中是否包含任何同步(如Exchange帐户或Google帐户)。如果是这样,则需要一段时间才能同步和删除。 – 2013-04-16 20:17:25

+1

自从两个月以来,我一直面临这个问题,两个月前我添加的数据仍然存在(已删除状态)。我不认为这是一个时间问题。 – 2013-04-16 22:25:12

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

这是一个很好的机会,银河S3至少有Android 4.0。这种改变可能会导致在S1上工作的代码,而不是S3。

+1

这不是一个URI问题。我已经使用a/m URI找到了数据,如果从S-Planner中删除,它仍然可以在Cursor中看到。我试着改变URI,但我得到相同的输出。 – 2013-03-19 11:12:48

+0

好的。我想我的一个观点是,如果你不是> = 4.0并且不使用新的API,那么Google说好运,你的代码可能会在某个时候破坏。我知道这对你没有太大帮助,但这是他们的官方声明。 – Shellum 2013-03-19 15:29:33

+1

感谢您的支持,但我实际上已尝试几乎所有内容,包括为每个SDK指定URI。 – 2013-03-19 17:15:55

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

它适用于所有设备。