2012-11-05 33 views
6

Hi tôi có thể nhận được các sự kiện sử dụng mã nàyCách nhận tất cả các sự kiện từ lịch?

cur = null; 
    cr = getContentResolver(); 


    // Construct the query with the desired date range. 
    Uri.Builder builder = Instances.CONTENT_URI.buildUpon(); 

    ContentUris.appendId(builder, 0); 
    ContentUris.appendId(builder, endMillis); 


    // Submit the query 
    cur = cr.query(builder.build(), 
     INSTANCE_PROJECTION, 
     null, 
     null, 
     null); 

nhưng như bạn thấy tôi có thể chỉ nhận được các sự kiện trong thời hạn thời gian nhất định, nhưng tôi cần tất cả các sự kiện (không có bất kỳ đặc điểm kỹ thuật thời gian không bao gồm các sự kiện lặp đi lặp lại) là điều này có thể? và làm thế nào ? hãy giúp tôi.

cur = cr.query(Uri.parse("content://com.android.calendar/events"), 
     INSTANCE_PROJECTION, 
     null, 
     null, 
     null); 

nó mang lại cho các java.lang.IllegalArgumentException lỗi:

+0

là những gì Instances ? – njzk2

Trả lời

10

Hãy thử mã này ...

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

Bạn cũng có thể kiểm tra điều này

Getting events from calendar

+0

cảm ơn Amit nhưng không hoạt động xem câu hỏi đã chỉnh sửa của tôi ...... –

+0

http://stackoverflow.com/questions/4302209/android-calendar-events hãy xem điều này –

+0

Cách nhận Listofattendees bằng Lịch – Adevelopment

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