2010-11-29 197 views
6

我只是在使用Calendar Content Provider進行操作,現在我無法顯示特定日期的事件。Android日曆事件

我知道活動的URI < 2.1版本和2.2版本,如下:

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 

我的疑惑是:

  1. 如何抓取所有的事件?
  2. 如何獲取特定日期的事件?

所以,請有人瞭解日曆請幫助我,並分享你的知識。

感謝名單

+0

檢查:HTTP:// stackoverflow.com/questions/26844770/how-to-get-access-to-the-calendars-on-a-android-phone – 2014-11-11 09:13:41

回答

9

這些實施例是用於< = 2.1版本;

第一;找出哪些日曆存在

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

抓取的所有事件,以及特定事件是通過指定範圍
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); 

,然後讓我們說你想從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非常感謝您的支持,但我對此行有所懷疑「/ instances/when 「,你寫了這個ContentUris.appendId(構建器,現在 - DateUtils.MILLIS_PER_DAY * 10000)和ContentUris.appendId(構建器,現在+ DateUtils.MILLIS_PER_DAY * 10000);線,但我如何獲取特定日期的事件? – 2010-11-29 10:03:17

+1

@ bbaja42 MILLIS_PER_DAY不適用於ANDROID – 2010-11-29 12:12:46

0

使用此代碼來獲取日常活動日曆日誌事件ID,

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