2010-11-29 10 views
6

Estoy haciendo una operación con Calendar Content Provider, ahora estoy fallando en el punto de mostrar eventos para la fecha en particular.Android Calendar Events

sé los Eventos URI para la versión < 2.1 y la versión 2.2, de la siguiente manera:

eventsUri = Uri.parse("content://calendar/events"); // < Android 2.1 version 

eventsUri = Uri.parse("content://com.android.calendar/events"); // For Android Froyo 2.2 and later version 

Mis dudas son:

  1. ¿Cómo obtener todos los eventos?
  2. ¿Cómo obtengo eventos para la fecha en particular?

Así que por favor, alguien con conocimiento sobre el Calendario por favor ayúdame y comparte tu conocimiento al respecto.

Thanx

+0

cheque: http: // stackoverflow.com/questions/26844770/how-to-get-access-to-the-calendars-on-a-android-phone –

Respuesta

9

Estos ejemplos son para < = 2.1 versión;

first; averiguar qué existen calendarios

Cursor cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null); 
cursor.moveToFirst(); 
String[] CalNames = new String[cursor.getCount()]; 
int[] CalIds = new int[cursor.getCount()]; 
for (int i = 0; i < CalNames.length; i++) { 
    CalIds[i] = cursor.getInt(0); 
    CalNames[i] = cursor.getString(1); 
    cursor.moveToNext(); 
} 
cursor.close(); 

Obtención de todos los eventos, y evento en particular se realiza mediante la especificación de gama
ContentResolver contentResolver = getContentResolver();

Uri.Builder builder = Uri.parse(getCalendarUriBase() + "/instances/when").buildUpon(); 
     long now = new Date().getTime(); 
     ContentUris.appendId(builder, now - DateUtils.MILLIS_PER_DAY*10000); 
     ContentUris.appendId(builder, now + DateUtils.MILLIS_PER_DAY * 10000); 

y luego digamos que desea registrar sucesos ID de calendario con ID = 1

Cursor eventCursor = contentResolver.query(builder.build(), 
       new String[] { "event_id"}, "Calendars._id=" + 1, 
       null, "startDay ASC, startMinute ASC"); 
     // For a full list of available columns see http://tinyurl.com/yfbg76w 
     while (eventCursor.moveToNext()) { 
      String uid2 = eventCursor.getString(0); 
      Log.v("eventID : ", uid2); 

     } 
+0

@ bbaja42 Gracias mucho por el soporte, pero tengo dudas con respecto a esta línea "/ instances/when ", donde ha escrito este ContentUris.appendId (constructor, ahora - DateUtils.MILLIS_PER_DAY * 10000) y ContentUris.appendId (constructor, ahora + DateUtils.MILLIS_PER_DAY * 10000); líneas, pero ¿cómo obtengo el evento solo para la FECHA EN PARTICULAR? –

+1

@ bbaja42 MILLIS_PER_DAY no está disponible con ANDROID –

+0

@ pm-paresh-mayani tiene usted razón; he usado una biblioteca adicional para ese – bbaja42

0

Use este código para obtener los acontecimientos diarios,

public static void readCalendarEvent(Context context) throws ParseException { 

     ContentResolver contentResolver = context.getContentResolver(); 
     Calendar calendar = Calendar.getInstance(); 
     String dtstart = "dtstart"; 
     String dtend = "dtend"; 


     SimpleDateFormat displayFormatter = new SimpleDateFormat("MMMM dd, yyyy (EEEE)"); 

     stime=displayFormatter.format(calendar.getTime());  

     SimpleDateFormat startFormatter = new SimpleDateFormat("MM/dd/yy"); 
     String dateString = startFormatter.format(calendar.getTime()); 

     long after = calendar.getTimeInMillis(); 
     SimpleDateFormat formatterr = new SimpleDateFormat("hh:mm:ss MM/dd/yy"); 
     Calendar endOfDay = Calendar.getInstance(); 
     Date dateCCC = formatterr.parse("23:59:59 " + dateString); 
     endOfDay.setTime(dateCCC); 






    cursor = contentResolver.query(Uri.parse("content://com.android.calendar/events"), (new String[] { "calendar_id", "title", "description", "dtstart", "dtend","eventTimezone", "eventLocation" }), "(" + dtstart + ">" + after + " and " + dtend + "<" + endOfDay.getTimeInMillis() + ")", null, "dtstart ASC"); 


     /*String[] COLS={"calendar_id", "title", "description", "dtstart", "dtend","eventTimezone", "eventLocation"}; 

     cursor = contentResolver.query(


       CalendarContract.Events.CONTENT_URI, COLS,null, null, null);*/ 

     gCalendar = new ArrayList<GoogleCalendar>(); 
     try { 


      if (cursor.getCount() > 0) { 


       while (cursor.moveToNext()) { 
        GoogleCalendar googleCalendar = new GoogleCalendar(); 
        gCalendar.add(googleCalendar); 
        int calendar_id = cursor.getInt(0); 
        googleCalendar.setCalendar_id(calendar_id); 
        String title = cursor.getString(1); 
        googleCalendar.setTitle(title); 
        String description = cursor.getString(2); 
        googleCalendar.setDescription(description); 
        String dtstart1 = cursor.getString(3); 
        dt=convertDate(dtstart1,"hh:mm:ss"); 
        googleCalendar.setDtstart(dt);          
        String dtend1 = cursor.getString(4); 
        googleCalendar.setDtend(dtend1);                
        String eventTimeZone=cursor.getString(5); 
        googleCalendar.setEventTimeZone(eventTimeZone); 
        String eventlocation = cursor.getString(6);     
        googleCalendar.setEventlocation(eventlocation); 

       } 
      } 
     } catch (AssertionError ex) { 
      ex.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

¿Qué es GoogleCalendar? –