アプリでユーザーのカレンダーにリマインダーを追加しようとしています。コードは を検索してtitle
、start date
イベントを追加する前に予定が既にカレンダーに存在するかどうかを確認します (重複を避けるため)。私の問題は、カレンダーからイベントを手動で (カレンダーを使用して) 削除すると、イベントがカレンダーから消えます (すべてのカレンダーを表示していて、カレンダー アプリケーションで見ることができません) が、データベースからではありません。私が理解できないもう1つのことは、プログラムでイベントを削除しようとしたことですが、それでも削除されません。
ここに私のコードスニペットがあります:
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 Samsung Galaxy S1 でコードをテストしましたが、正常に動作しています。Samsung Galaxy S3 の問題のようです (私の問題は、S-planner アプリのバグだと思います)。