27

アプリでユーザーのカレンダーにリマインダーを追加しようとしています。コードは を検索してtitlestart 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 アプリのバグだと思います)。

4

3 に答える 3

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 では動作していません。

于 2013-03-19T01:53:57.673 に答える
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;
}

すべてのデバイスで動作します。

于 2017-09-14T03:27:36.990 に答える