2012-02-08 60 views
5

Tôi đã thêm một sự kiện lịch được lập trình bằng cách sử dụng api caledarcontract và nhận được một eventId. Tương tự như vậy tôi đã thêm một lời nhắc nhở cho sự kiện này và lưu lời nhắc nhở quá. Bây giờ tôi không muốn một lời nhắc nhở cho sự kiện này (hoặc tôi muốn tắt lời nhắc nhở), vì vậy tôi đang cố gắng để xóa các lời nhắc nhở bằng cách sử dụng remindId nhưng tôi không thể xóa. Tôi đã cố gắng để xóa các lời nhắc bằng cách sử dụng eventId quá nhưng nó không hoạt động.Không thể xóa lời nhắc từ Lịch trong Android

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; 

} 

Sau khi thực thi mã này mọi lúc, hàng trả về 0 có nghĩa là không có hàng nào bị thay đổi. Và lời nhắc đến chính xác vào thời điểm thích hợp. Làm cách nào để xóa lời nhắc khỏi lịch mà không xóa sự kiện?

+0

bất kỳ ngoại lệ hoặc lỗi nào trong khi xóa? – Sameer

+0

không. không có lỗi hoặc ngoại lệ. chỉ số lượng hàng đã thay đổi đang đến là 0 – Vansi

+0

Đi qua câu trả lời của tôi .. – Sameer

Trả lời

3

Tôi không chắc chắn mà SDK phiên bản bạn đang chạy chống lại khi thất bại, nhưng mã này (trong đó chủ yếu là giống như bạn, trừ đi kiểm tra phiên bản) làm việc cho tôi:

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

tôi reminderId bằng cách truy vấn lời nhắc của sự kiện:

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(); 
0

Đây có thể không phải là cách duy nhất hoặc tốt nhất, nhưng tất cả những gì tôi có thể tìm ra là cách xóa tất cả các lời nhắc lời nhắc cho một sự kiện. Tôi không biết cách nào để xóa chỉ một lời nhắc.

//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"} 
); 

Thật không may, điều này loại bỏ tất cả các nhắc nhở, nhưng tôi không chắc chắn nhiều nhắc nhở đang thực sự được hỗ trợ bởi Android 14 + hay nhất nhà cung cấp lịch (ví dụ hối). Ứng dụng lịch trong ICS chỉ cho phép thêm một lời nhắc (mặc dù nói "Thêm lời nhắc").

Và nếu tôi sử dụng một ứng dụng khác như Lịch doanh nghiệp để thêm nhiều lời nhắc, khi tôi kiểm tra trong Exchange, nó chỉ hiển thị lời nhắc. Nó hiển thị nhiều lời nhắc trong ứng dụng lịch nhưng chỉ trên thiết bị đó, chứ không phải trên các thiết bị khác, vì vậy nhiều lời nhắc dường như chỉ là địa phương.

Các vấn đề liên quan