2012-11-18 27 views
6

Tôi muốn lấy lại dữ liệu của sự kiện được thêm gần đây nhất từ lịch android. Tôi đang sử dụng mã này để có được id cuối cùng.Lấy dữ liệu về sự kiện được thêm gần đây nhất từ ​​lịch android

public static long getNewEventId(ContentResolver cr, Uri cal_uri) 
{ 
     Uri local_uri = cal_uri; 
     if (cal_uri == null) 
     { 
      local_uri = Uri.parse("content://com.android.calendar/events"); 
     } 
     Cursor cursor = cr.query(local_uri, 
       new String[] { "MAX(_id) as max_id" }, null, null, "_id"); 
     cursor.moveToFirst(); 
     long max_val = cursor.getLong(cursor.getColumnIndex("max_id")); 
     return max_val + 1; 
} 

Và sau đó tôi chỉ cần thêm một sự kiện sử dụng mã này:

Intent intent = new Intent(Intent.ACTION_EDIT); 
intent.setType("vnd.android.cursor.item/event"); 
intent.putExtra("beginTime", SelectedDate); 
intent.putExtra("allDay", false); 
intent.putExtra("rrule", "FsREQ=DAILY"); 
intent.putExtra("endTime", SelectedDate + 60 * 60 * 1000); 
intent.putExtra("title", "Advance Scheduler Event"); 
startActivity(intent); 

Sau này, tôi chỉ đơn giản là lấy dữ liệu của sự kiện này sử dụng mã này:

public CalendarData EventDetails(int ID) 
{ 
    CalendarData temp = null; 

    // ------------------------------------------------------------------------------- 

    ContentResolver cr = getContentResolver(); 

    Cursor cursor_calendar; 
    if (Integer.parseInt(Build.VERSION.SDK) >= 8) 
    { 
     cursor_calendar = cr.query(
       Uri.parse("content://com.android.calendar/calendars"), 
       new String[] { "_id", "displayname" }, null, null, null); 
    } 
    else 
    { 
     cursor_calendar = cr.query(
       Uri.parse("content://calendar/calendars"), new String[] { 
         "_id", "displayname" }, null, null, null); 
    } 
    cursor_calendar.moveToFirst(); 
    String[] CalNamess = new String[cursor_calendar.getCount()]; 
    int[] CalIdss = new int[cursor_calendar.getCount()]; 
    for (int i = 0; i < CalNamess.length; i++) 
    { 
     CalIdss[i] = cursor_calendar.getInt(0); 
     CalNamess[i] = cursor_calendar.getString(1); 
     cursor_calendar.moveToNext(); 
    } 
    cursor_calendar.close(); 

    // ------------------------------------------------------------------------------- 

    Cursor cursor_event; 
    if (Integer.parseInt(Build.VERSION.SDK) >= 8) 
    { 
     cursor_event = cr.query(
       Uri.parse("content://com.android.calendar/events"), 
       new String[] { "calendar_id", "title", "description", 
         "dtstart", "dtend", "eventLocation" }, null, null, 
       null); 
    } 
    else 
    { 
     cursor_event = cr.query(Uri.parse("content://calendar/events"), 
       new String[] { "calendar_id", "title", "description", 
         "dtstart", "dtend", "eventLocation" }, null, null, 
       null); 
    } 

    boolean flag = false; 
    String add = null; 
    cursor_event.moveToFirst(); 
    String[] CalNames = new String[cursor_event.getCount()]; 
    int[] CalIds = new int[cursor_event.getCount()]; 
    for (int i = 0; i < CalNames.length; i++) 
    { 
     CalIds[i] = cursor_event.getInt(0); 
     if (ID == CalIds[i]) 
     { 
      flag = true; 
      Toast.makeText(getApplicationContext(), 
        "ID Found : " + CalIds[i], Toast.LENGTH_LONG).show(); 
      CalNames[i] = "Event" 
        + cursor_event.getInt(0) 
        + ": \nTitle: " 
        + cursor_event.getString(1) 
        + "\nDescription: " 
        + cursor_event.getString(2) 
        + "\nStart Date: " 
         + cursor_event.getLong(cursor_event 
          .getColumnIndex("dtstart")) 
        + cursor_event.getLong(cursor_event 
          .getColumnIndex("dtend")) 
        + cursor_event.getString(5); 

      temp = new CalendarData(); 

      temp.Title = cursor_event.getString(1); 
      temp.Description = cursor_event.getString(2); 
      // temp.StartDate = new Date(cursor_event.getLong(3)); 
      // temp.EndDate = new Date(cursor_event.getLong(4)); 

      temp.StartDate = cursor_event.getLong(cursor_event 
        .getColumnIndex("dtstart")); 
      temp.EndDate = cursor_event.getLong(cursor_event 
        .getColumnIndex("dtend")); 
      temp.Location = cursor_event.getString(5); 
      break; 
     } 
     cursor_event.moveToNext(); 
    } 
    return temp; 
} 

Nhưng tôi có thể' t lấy dữ liệu của sự kiện này. Tôi không nhận được vấn đề ở đâu. Xin hãy giúp tôi giải quyết vấn đề này.

Trả lời

3

sử dụng mã này. hoạt động trong aap của tôi

public long GetMaxID(ContentResolver cr, Uri cal_uri, Context context) 
    { 
     Uri local_uri = cal_uri; 
     if (cal_uri == null) 
     { 
      // local_uri = Uri.parse("content://calendar/calendars/" + 
      // "events"); 
      local_uri = Uri.parse("content://com.android.calendar/events"); 
     } 
     Cursor cursor = cr.query(local_uri, new String[] 
     { "MAX(_id) as max_id" }, null, null, "_id"); 

     cursor.moveToFirst(); 
     long max_val = cursor.getLong(cursor.getColumnIndex("max_id")); 
     return max_val + 1; 

    } 

public static CalendarData GetEventDetails(String ID, Context context) 
    { 

     CalendarData temp = null; 
     ContentResolver contentResolver = context.getContentResolver(); 

     // Fetch a list of all calendars synced with the device, their display 
     // names and whether the 

     cursor = contentResolver.query(
       Uri.parse("content://com.android.calendar/calendars"), 
       (new String[] 
       { "_id", "displayName", "selected" }), null, null, null); 

     HashSet<String> calendarIds = new HashSet<String>(); 

     try 
     { 
      System.out.println("Count=" + cursor.getCount()); 
      if (cursor.getCount() > 0) 
      { 
       System.out 
         .println("the control is just inside of the cursor.count loop"); 
       while (cursor.moveToNext()) 
       { 

        String _id = cursor.getString(0); 
        String displayName = cursor.getString(1); 
        Boolean selected = !cursor.getString(2).equals("0"); 

        System.out.println("Id: " + _id + " Display Name: " 
          + displayName + " Selected: " + selected); 
        calendarIds.add(_id); 
       } 
      } 
     } 
     catch (AssertionError ex) 
     { 
      ex.printStackTrace(); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     // For each calendar, display all the events from the previous week to 
     // the end of next week. 
     // for (String id : calendarIds) 
     for (int id = 0; id <= calendarIds.size(); id++) 
     { 
      Uri.Builder builder = Uri.parse(
        "content://com.android.calendar/instances/when") 
        .buildUpon(); 
      // Uri.Builder builder = 
      // Uri.parse("content://com.android.calendar/calendars").buildUpon(); 
      long now = new Date().getTime(); 

      ContentUris 
        .appendId(builder, now - DateUtils.DAY_IN_MILLIS * 10000); 
      ContentUris 
        .appendId(builder, now + DateUtils.DAY_IN_MILLIS * 10000); 

      Cursor eventCursor = contentResolver.query(builder.build(), 
        new String[] 
        { "_id", "title", "begin", "end", "allDay" }, 
        "Calendars._id=" + id, null, null); 

      System.out.println(id + " eventCursor count=" 
        + eventCursor.getCount()); 
      if (eventCursor.getCount() > 0) 
      { 

       eventCursor.moveToFirst(); 

       while (eventCursor.moveToNext()) 
       { 
        Object mbeg_date, beg_date, beg_time, end_date, end_time; 

        final String eventID = eventCursor.getString(0); 
        final String title = eventCursor.getString(1); 
        final Date begin = new Date(eventCursor.getLong(2)); 
        final Date end = new Date(eventCursor.getLong(3)); 
        final Boolean allDay = !eventCursor.getString(4) 
          .equals("0"); 

        if (eventID.equals(ID)) 
        { 
         temp = new CalendarData(); 
         temp.Title = eventCursor.getString(1); 
         temp.StartDate = eventCursor.getLong(2); 
         temp.EndDate = eventCursor.getLong(3); 

         break; 
        } 

       } 
      } 
      // break; 

     } 
     return temp; 
    } 
+1

cảm ơn bạn rất nhiều :) –

2

tôi đã sử dụng đoạn mã sau để lấy tất cả các dữ liệu trên Lịch đó là:

cr = getApplicationContext().getContentResolver(); 
     caluri=CalendarContract.Events.CONTENT_URI; 
     atteuri=CalendarContract.Attendees.CONTENT_URI; 
try 
      { 
       cur1 = cr.query(caluri, new String[]{Events.CALENDAR_ID,Events._ID, Events.TITLE, Events.DESCRIPTION,Events.DTSTART, Events.DTEND, Events.EVENT_LOCATION }, null, null, null);   
        if(cur1!=null){ 
         while(cur1.moveToNext()){ 
          cal_ID=cur1.getString(cur1.getColumnIndex(Events.CALENDAR_ID)); 
          event_ID=cur1.getString(cur1.getColumnIndex(Events._ID)); 
          cur2=cr.query(atteuri,new String[]{Attendees.ATTENDEE_NAME,Attendees.ATTENDEE_EMAIL}, Attendees.EVENT_ID +"=" +event_ID, null, null); 
          if(cur2!=null){ 
           while(cur2.moveToNext()){ 
            event_Title=cur1.getString(cur1.getColumnIndex(Events.TITLE)); 
            event_Desc=cur1.getString(cur1.getColumnIndexOrThrow(Events.DESCRIPTION)); 
            event_Start=new Date(cur1.getLong(cur1.getColumnIndex(Events.DTSTART))); 
            event_end=new Date(cur1.getLong(cur1.getColumnIndex(Events.DTEND))); 
            event_loc=cur1.getString(cur1.getColumnIndex(Events.EVENT_LOCATION)); 
            attendee_name=cur2.getString(cur2.getColumnIndex(Attendees.ATTENDEE_NAME)); 
            attendee_Email=cur2.getString(cur2.getColumnIndex(Attendees.ATTENDEE_EMAIL)); 
            all_attendee +="\n"+attendee_name; 
            all_Emails +="\n"+attendee_Email; 
           } 
           cur2.close(); 
          } 
          all +="Event title: " + event_Title + "\n" + "Event Description: " + event_Desc + "\n" +"Event Start: " + event_Start + "\n" + "Events End: " + event_end + "\n" + "Event Location: " + event_loc + "\n" + "Attendees: " + "\n" + all_attendee + "\n" + "Emails: "+ "\n" + all_Emails + "\n"; 
         } 
         cur1.close(); 
        } 
      } 
      catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 

SO tất cả những gì bạn cần như tôi đoán là để điều chỉnh nó một chút có được dữ liệu cho sự kiện cuối cùng.

+0

tôi không sử dụng 4.0. tôi yêu cầu 2.3 –

+0

Ok tại sao bạn không chỉ định rằng trong câu hỏi của bạn –

0
ContentResolver cr = getContentResolver(); 
      Uri caluri = CalendarContract.Events.CONTENT_URI; 
      Uri atteuri = CalendarContract.Attendees.CONTENT_URI; 
      Cursor cur1, cur2; 
      String all = null; 
      try 
      { 
       cur1 = cr.query(caluri 
        , new String[]{ Events.CALENDAR_ID, Events._ID, Events.TITLE, Events.DESCRIPTION, Events.DTSTART, Events.DTEND, Events.EVENT_LOCATION } 
        , null, null, null); 
       if (cur1 != null) 
       { 
        while (cur1.moveToNext()) 
        { 
         String event_Title = cur1.getString(cur1.getColumnIndex(Events.TITLE)); 
         String event_Desc = cur1.getString(cur1.getColumnIndexOrThrow(Events.DESCRIPTION)); 
         Date event_Start = new Date(cur1.getLong(cur1.getColumnIndex(Events.DTSTART))); 
         Date event_end = new Date(cur1.getLong(cur1.getColumnIndex(Events.DTEND))); 
         String event_loc = cur1.getString(cur1.getColumnIndex(Events.EVENT_LOCATION)); 
         String all_attendee = null; 
         String all_Emails = null; 

         String cal_ID = cur1.getString(cur1.getColumnIndex(Events.CALENDAR_ID)); 
         String event_ID = cur1.getString(cur1.getColumnIndex(Events._ID)); 
         cur2 = cr.query(atteuri, new String[]{ Attendees.ATTENDEE_NAME, Attendees.ATTENDEE_EMAIL } 
          , Attendees.EVENT_ID + "=" + event_ID, null, null); 
         if (cur2 != null) 
         { 
          while (cur2.moveToNext()) 
          { 
           String attendee_name = cur2.getString(cur2.getColumnIndex(Attendees.ATTENDEE_NAME)); 
           String attendee_Email = cur2.getString(cur2.getColumnIndex(Attendees.ATTENDEE_EMAIL)); 

           all_attendee += "\n" + attendee_name; 
           all_Emails += "\n" + attendee_Email; 
          } 
          cur2.close(); 
         } 
         all += "Event title: " + event_Title + "\n" 
           + "Event Description: " + event_Desc + "\n" 
           + "Event Start: " + event_Start + "\n" + "Events End: " 
           + event_end + "\n" + "Event Location: " + event_loc 
           + "\n" + "Attendees: " + "\n" + all_attendee + "\n" 
           + "Emails: " + "\n" + all_Emails + "\n"; 
        } 
        cur1.close(); 
       } 
       System.out.println("My log--------" + all); 
Các vấn đề liên quan