2013-02-20 69 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; 
} 

它適用於所有設備。