5

caledarcontract API を使用してプログラムでカレンダー イベントを追加し、eventId を取得しました。同様に、このイベントのリマインダーを追加し、reminderId も保存しました。このイベントのリマインダーが必要ない (またはリマインダーをオフにしたい) ので、reminderId を使用してリマインダーを削除しようとしていますが、削除できません。eventId も使用してリマインダーを削除しようとしましたが、機能しません。

public int AddEventToCalendar(String calendarId, Entity entity) {
    // TODO Auto-generated method stub
    ContentValues event = new ContentValues();
    event.put("calendar_id", calendarId);
    event.put("title", entity.description);
    event.put("dtstart", System.currentTimeMillis());
    event.put("dtend", System.currentTimeMillis() + 3600*1000);
    event.put("allDay", 0);
    //status: 0~ tentative; 1~ confirmed; 2~ canceled
    event.put("eventStatus", 1);
    //0~ default; 1~ confidential; 2~ private; 3~ public
    event.put("visibility", 0);
    //0~ opaque, no timing conflict is allowed; 1~ transparency, allow overlap of scheduling
    event.put("transparency", 0);
    //0~ false; 1~ true
    event.put("hasAlarm", 1);
    Uri add_eventUri;
    if (Build.VERSION.SDK_INT >= 8) {
        add_eventUri = Uri.parse("content://com.android.calendar/events");
    } else {
        add_eventUri = Uri.parse("content://calendar/events");
    }
    Uri l_uri = context.getContentResolver().insert(add_eventUri, event);
    if(l_uri != null)
    {
        long eventID = Long.parseLong(l_uri.getLastPathSegment());
        return (int) eventID;
    }
    else
        return 0;
}

public int AddReminderOnEvent(Entity entity)
{
    if(entity.eventId != 0)
    {
        ContentValues reminderValues = new ContentValues();
        reminderValues.put("event_id", entity.eventId);
        reminderValues.put("method", 1);// will alert the user with a reminder notification
        reminderValues.put("minutes", 0);// number of minutes before the start time of the event to fire a reminder
        Uri reminder_eventUri;
        if (Build.VERSION.SDK_INT >= 8) {
            reminder_eventUri = Uri.parse("content://com.android.calendar/reminders");
        } else {
            reminder_eventUri = Uri.parse("content://calendar/reminders");
        }
        Uri r_uri = context.getContentResolver().insert(reminder_eventUri, reminderValues); 
        if(r_uri != null)
        {
            long reminderID = Long.parseLong(r_uri.getLastPathSegment());
            return (int) reminderID;
//          Toast.makeText(getApplicationContext(), "Event Created Successfully", Toast.LENGTH_LONG).show();
        }
        else
            return 0;
    }
    else
    {
        return 0;
    }
}

    public boolean DeleteReminderOnTask(int eventId, int reminderId) {
    // TODO Auto-generated method stub

    Uri delete_reminderUri;
    if (Build.VERSION.SDK_INT >= 8) {
        delete_reminderUri = Uri.parse("content://com.android.calendar/reminders");
    } else {
        delete_reminderUri = Uri.parse("content://calendar/reminders");
    }
    delete_reminderUri = ContentUris.withAppendedId(delete_reminderUri, reminderId);
    int rows = context.getContentResolver().delete(delete_reminderUri,null , null);

    if(rows > 0)
        return true;
    else
        return false;

}

行が変更されていないことを意味する 0 を返すたびに、このコードを実行した後。そして、リマインダーは適切なタイミングで正確に表示されます。イベントを削除せずにカレンダーからリマインダーを削除するには?

4

2 に答える 2

3

失敗したときに実行しているSDKのバージョンはわかりませんが、このコード(基本的にあなたのものと同じですが、バージョンチェックを除いたものです)は私にとってはうまくいきます:

Uri reminderUri = ContentUris.withAppendedId(
    CalendarContract.Reminders.CONTENT_URI, reminderId);
int rows = contentResolver.delete(reminderUri, null, null);

reminderIdイベントのリマインダーを照会して取得しました:

    String[] projection = new String[] {
            CalendarContract.Reminders._ID,
            CalendarContract.Reminders.METHOD,
            CalendarContract.Reminders.MINUTES
    };

    Cursor cursor = CalendarContract.Reminders.query(
        contentResolver, eventId, projection);
    while (cursor.moveToNext()) {
        long reminderId = cursor.getLong(0);
        int method = cursor.getInt(1);
        int minutes = cursor.getInt(2);

        // etc.

    }
    cursor.close();
于 2013-05-02T01:59:55.377 に答える
0

これが唯一または最良の方法ではないかもしれませんが、私が理解できたのは、イベントのすべてのリマインダーを削除する方法だけでした. リマインダーを 1 つだけ削除する方法がわかりません。

//What we want to update
ContentValues values = new ContentValues();
values.put(Events.HAS_ALARM, 0);

//We're setting the event to have no alarms
int result = getContentResolver().update(
    Events.CONTENT_URI,
    values,
    Events._ID + " = ?",
    new String[]{"44"}
);

残念ながら、これによりすべてのリマインダーが削除されますが、複数のリマインダーが Android 14 以降またはほとんどのカレンダー プロバイダー (Exchange など) で実際にサポートされているかどうかはわかりません。ICS のカレンダー アプリでは、リマインダーを 1 つしか追加できません (「リマインダーを追加」と表示されているにもかかわらず)。

また、ビジネス カレンダーなどの別のアプリケーションを使用して複数のリマインダーを追加すると、Exchange にチェックインするとリマインダーが 1 つしか表示されません。カレンダー アプリには複数のリマインダーが表示されますが、他のデバイスではなくそのデバイスでのみ表示されるため、複数のリマインダーはローカルのみのようです。

于 2013-02-26T07:55:45.847 に答える