2012-02-08 6 views
5

He agregado un evento de calendario programáticamente utilizando la API de caledarcontract y obtuve un Id de evento. Del mismo modo, agregué un recordatorio para este evento y también guardé el recordatorio. Ahora no quiero un recordatorio para este evento (o me gustaría desactivar el recordatorio), así que estoy tratando de eliminar el recordatorio usando el recordatorio pero no puedo eliminarlo. Traté de eliminar el recordatorio usando eventId pero no está funcionando.No se ha podido eliminar un recordatorio de Calendar en 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; 

} 

Después de ejecutar este código cada vez que las filas vuelvan a 0 significa que no se han alterado las filas. Y el recordatorio aparece exactamente en el momento apropiado. ¿Cómo eliminar el recordatorio del calendario sin borrar el evento?

+0

¿Alguna excepción o error al eliminar? – Sameer

+0

no. no hay error o excepción solo el recuento de las filas alteradas viene como 0 – Vansi

+0

Ir a través de mi respuesta .. – Sameer

Respuesta

3

no estoy seguro de qué versión del SDK que se está ejecutando en contra al fallar, pero este código (que es esencialmente el mismo que el suyo, menos la comprobación de versión) funciona para mí:

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

llegué reminderId consultando los recordatorios del evento:

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

Esta podría no ser la única o la mejor manera, pero todo lo que pude averiguar fue cómo eliminar todos los recordatorios para un evento. No sé de una forma de eliminar solo un recordatorio.

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

Desafortunadamente, esto elimina todos los recordatorios, pero no estoy seguro de múltiples recordatorios son realmente compatibles con Android 14+ o la mayoría de los proveedores de calendario (por ejemplo, Exchange). La aplicación de calendario en ICS solo permite agregar un recordatorio (a pesar de decir "Agregar recordatorios").

Y si utilizo otra aplicación como Business Calendar para agregar varios recordatorios, cuando reviso en Exchange, solo muestra los recordatorios. Muestra múltiples recordatorios en la aplicación de calendario, pero solo en ese dispositivo, no en otros dispositivos, por lo que los recordatorios múltiples parecen como locales solamente.

Cuestiones relacionadas