2012-04-09 37 views
8

Tôi muốn nhận sự kiện lịch cuối cùng trên các thiết bị Android? tôi mới bắt đầu nên cho tôi biết từng bước giải pháp cho điều này ..! Chỉ nhận và đọc lịch không cập nhật và xóa. Có ai giúp tôi không? tôi đã sử dụng mã ở bên dưới!cách tải danh sách sự kiện lịch trên thiết bị trong thiết bị 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); 
      } 
     } 
    } 


} 

i got null-pointerException.

+1

Thậm chí bạn đang newbie, tôi chắc chắn bạn không phải là newbie trong "google search" :) Vui lòng kiểm tra thi s đã tồn tại câu hỏi: [Sự kiện Lịch Android] (http://stackoverflow.com/questions/4302209/android-calendar-events) –

+0

paresh bạn giúp tôi !! tôi đã thử mã này được sử dụng. – Hiteshpatel0024

+0

Cách nhận sự kiện cho tài khoản lịch google? – Deepak

Trả lời

33

Vấn đề của tôi được giải quyết và tôi sử dụng dưới đây 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

Câu trả lời của bạn là tuyệt vời. Vui lòng liên hệ với tôi cách lấy id đó ("calendar_id"). tôi đã thử như thế này id = cursor.getString (0); nhưng nó cung cấp cho 2 cho tất cả các sự kiện. – AndroidRaji

+0

bạn đã sử dụng để liên kết nhiều trợ giúp cho bạn: http://www.grokkingandroid.com/androids-calendarcontract-provider/ – Hiteshpatel0024

+0

Làm cách nào để nhận các sự kiện lịch cho ngày hoặc tuần cụ thể? – Deepak

1

lớn Cảm ơn! Tuy nhiên, bạn có tất cả các sự kiện chỉ từ một Lịch, như Tài khoản Google chính của mình, bạn có thể nhận sự kiện từ tất cả các lịch có sẵn như thế nào ??

Lưu ý rằng trên Tiện ích lịch được tạo sẵn của bạn, bạn có thể xem các sự kiện từ Gmail, Exchange, Facebook v.v. ' Cách nhận các sự kiện này?

Đối với các sự kiện đặc biệt cho ngày hoặc tuần, tôi đã làm điều này bằng cách sử dụng các mệnh đề lựa chọn và lựa chọn args trong báo cáo kết quả truy vấn như thế này cho đặc biệt trong ngày:

Đây là những lựa chọn và args xác định:

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

Và điều này như thế nào truy vấn tìm:

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

Bạn có thể đăng một mã làm việc cho điều này không? Nó sẽ giúp ích rất nhiều. –

Các vấn đề liên quan