2013-01-20 4 views
3

아래 코드를 사용하여 내 애플리케이션에서 캘린더 일정을 가져오고로드합니다. 완벽하게 작동합니다. 하지만 지금은 이벤트 날짜 범위를 지정하고 싶습니다. 어떻게 그것을 얻을 수 있습니다 ..날짜 범위 또는 월별 캘린더 일정을 android에서 가져 오는 방법

Cursor cursor = getContentResolver().query(
       Uri.parse("content://com.android.calendar/events"), 
       new String[] { "_id", "title", "dtstart", "dtend" }, null, 
       null, "dtstart ASC"); 


DTSTART해야 =이 내가를 검색하기 위해 구현 된 것 "2013-01-01"
DTEND = "2013-01-31"

답변

3

했다 이벤트 :

if (Integer.parseInt(Build.VERSION.SDK) >= 8 || Integer.parseInt(Build.VERSION.SDK) <= 13) { 

      uri = "content://com.android.calendar/events"; 
      CALENDAR_URI = Uri.parse(uri); 

     } else if(Integer.parseInt(Build.VERSION.SDK) >= 14){ 

      CALENDAR_URI = CalendarContract.Events.CONTENT_URI; 

     } 
     else { 

      uri = "content://calendar/events"; 
      CALENDAR_URI = Uri.parse(uri); 

     } 
Cursor cursors = context.getContentResolver().query(CALENDAR_URI, new String[]{ "_id", "title", "description", "dtstart", "dtend", "eventLocation" }, 
       null,null, null); 



cursors.moveToFirst(); 
            String[] CalNames = new String[cursors.getCount()]; 
            int[] CalIds = new int[cursors.getCount()]; 
            for (int i = 0; i < CalNames.length; i++) { 
             CalIds[i] = cursors.getInt(0); 
             CalNames[i] = "Event"+cursors.getInt(0)+": \nTitle: "+ cursors.getString(1)+"\nDescription: "+cursors.getString(2)+"\nStart Date: "+new Date(cursors.getLong(3))+"\nEnd Date : "+new Date(cursors.getLong(4))+"\nLocation : "+cursors.getString(5); 

             Date mDate = new Date(cursors.getLong(3)); 
             Date nDate = new Date(cursors.getLong(4)); 

             long mTime = mDate.getTime(); 
             long lTime = nDate.getTime(); 
             if(stTime <= mTime && enTime >= lTime){ 
String eid = cursors.getString(0); 

              int eID = Integer.parseInt(eid); 

              String desc = cursors.getString(2); 
              String title = cursors.getString(1); 

이 경우 stTime은 ydtstart이고 enTime은 사용자의 dtend입니다.

+0

검사 : http://stackoverflow.com/questions/26844770/how-to-get-access-to-the-calendars-on-a-android-phone –

12
String[] projection = new String[] { CalendarContract.Events.CALENDAR_ID, CalendarContract.Events.TITLE, CalendarContract.Events.DESCRIPTION, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.ALL_DAY, CalendarContract.Events.EVENT_LOCATION }; 

// 0 = January, 1 = February, ... 

Calendar startTime = Calendar.getInstance(); 
startTime.set(2014,00,01,00,00); 

Calendar endTime= Calendar.getInstance(); 
endTime.set(2015,00,01,00,00); 

// the range is all data from 2014 

String selection = "((" + CalendarContract.Events.DTSTART + " >= " + startTime.getTimeInMillis() + ") AND (" + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + "))"; 

Cursor cursor = this.getBaseContext().getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, null, null); 

// output the events 

if (cursor.moveToFirst()) { 
    do { 
     Toast.makeText(this.getApplicationContext(), "Title: " + cursor.getString(1) + " Start-Time: " + (new Date(cursor.getLong(3))).toString(), Toast.LENGTH_LONG).show(); 
    } while (cursor.moveToNext()); 
} 
+1

thnx는, u는 내 시간을 저장. –

+0

;) 당신이 대답을 upvote 수 있다면 좋은 것입니다 –

+1

예, Erdinc, 일찍하려고하지만, 내 명성이 낮습니다. 그러나 좋은 도움. –

관련 문제