2012-04-09 73 views
8

我想在android設備上獲取日曆事件最後七天?我是初學者,所以告訴我一步一步的解決方案!!只有獲取和閱讀日曆不更新和刪除。有人幫助我嗎?我用下面的代碼!如何在Android設備中獲取設備日曆事件列表?

public class Example { 

    public static void readCalendar(Context context) { 

     ContentResolver contentResolver = context.getContentResolver(); 

     // Fetch a list of all calendars synced with the device, their display names and whether the 
     // user has them selected for display. 

     final Cursor cursor = contentResolver.query(Uri.parse("content://calendar/calendars"), 
       (new String[] { "_id", "displayName", "selected" }), null, null, null); 
     // For a full list of available columns see http://tinyurl.com/yfbg76w 

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

     while (cursor.moveToNext()) { 

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

      Log.v("anim","Id: " + _id + " Display Name: " + displayName + " Selected: " + selected); 
      calendarIds.add(_id); 
     } 

     // For each calendar, display all the events from the previous week to the end of next week.   
     for (String id : calendarIds) { 
      Uri.Builder builder = Uri.parse("content://calendar/instances/when").buildUpon(); 
      long now = new Date().getTime(); 
      ContentUris.appendId(builder, now - DateUtils.WEEK_IN_MILLIS); 
      ContentUris.appendId(builder, now + DateUtils.WEEK_IN_MILLIS); 

      Cursor eventCursor = contentResolver.query(builder.build(), 
        new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + id, 
        null, "startDay ASC, startMinute ASC"); 
      // For a full list of available columns see http://tinyurl.com/yfbg76w 

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

       Log.v("anim","Title: " + title + " Begin: " + begin + " End: " + end + 
         " All Day: " + allDay); 
      } 
     } 
    } 


} 

我得到了null-pointerException。

+1

即使你是新手,我相信你是不是在「Google搜索」 :)請檢查THI一個新手已經存在的問題:[Android日曆事件](http://stackoverflow.com/questions/4302209/android-calendar-events) – 2012-04-09 05:38:06

+0

帕尼斯你幫助我!我嘗試使用此代碼。 – Hiteshpatel0024 2012-04-16 10:25:59

+0

如何獲取Google日曆帳戶的活動? – Deepak 2013-11-27 07:40:28

回答

33

我的問題得到解決,我用下面的code -

public class Utility { 
    public static ArrayList<String> nameOfEvent = new ArrayList<String>(); 
    public static ArrayList<String> startDates = new ArrayList<String>(); 
    public static ArrayList<String> endDates = new ArrayList<String>(); 
    public static ArrayList<String> descriptions = new ArrayList<String>(); 

    public static ArrayList<String> readCalendarEvent(Context context) { 
     Cursor cursor = context.getContentResolver() 
       .query(
         Uri.parse("content://com.android.calendar/events"), 
         new String[] { "calendar_id", "title", "description", 
           "dtstart", "dtend", "eventLocation" }, null, 
         null, null); 
     cursor.moveToFirst(); 
     // fetching calendars name 
     String CNames[] = new String[cursor.getCount()]; 

     // fetching calendars id 
     nameOfEvent.clear(); 
     startDates.clear(); 
     endDates.clear(); 
     descriptions.clear(); 
     for (int i = 0; i < CNames.length; i++) { 

      nameOfEvent.add(cursor.getString(1)); 
      startDates.add(getDate(Long.parseLong(cursor.getString(3)))); 
      endDates.add(getDate(Long.parseLong(cursor.getString(4)))); 
      descriptions.add(cursor.getString(2)); 
      CNames[i] = cursor.getString(1); 
      cursor.moveToNext(); 

     } 
     return nameOfEvent; 
    } 

    public static String getDate(long milliSeconds) { 
     SimpleDateFormat formatter = new SimpleDateFormat(
       "dd/MM/yyyy hh:mm:ss a"); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(milliSeconds); 
     return formatter.format(calendar.getTime()); 
    } 
+0

您的回答非常好。請告訴我如何獲得該id(「calendar_id」)。我試過像這樣id = cursor.getString(0);但它爲所有事件提供2。 – AndroidRaji 2012-12-26 10:22:14

+0

您用於鏈接很多幫助爲您:http://www.grokkingandroid.com/androids-calendarcontract-provider/ – Hiteshpatel0024 2013-01-04 09:39:57

+0

如何獲得特定日期或星期的日曆事件? – Deepak 2013-11-08 05:54:06

1

十分感謝! 但是,您僅從一個日曆中獲取了所有活動,例如您的主Google帳戶,如何從所有可用日曆中獲取活動?

請注意,在您的內置日曆窗口小部件中,您可以看到來自Gmail,Exchange,Facebook等的事件' 如何獲取這些事件?

至於日期或每週特定的事件,我這樣做是通過使用 在查詢語句中的選擇條款和選擇參數數量像這樣特別的日子:

這些都是選擇和args定義:

Uri content = Uri.parse("content://com.android.calendar/events"); 
String[] vec = new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "allDay", "eventLocation" }; 
String selectionClause = "(dtstart >= ? AND dtend <= ?) OR (dtstart >= ? AND allDay = ?)"; 
String[] selectionsArgs = new String[]{"" + dtstart, "" + dtend, "" + dtstart, "1"}; 

而這種查詢的樣子:

ContentResolver contentResolver = context.getContentResolver(); 
    Cursor cursor = contentResolver.query(content, vec, selectionClause, selectionsArgs, null); 
+0

您能否爲此發佈工作代碼?這將有很大的幫助。 – 2017-03-23 07:35:52