Android Calendar Events

后端 未结 3 877
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 10:47

I am just making an operation with Calendar Content Provider, now I am failing at the point to display events for the particular date.

I know the Events URI for the

相关标签:
3条回答
  • 2020-11-29 11:05

    These examples are for <= 2.1 version;

    first; find out which calendars exist

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

    Fetching all events, and particular event is done by specifying range
    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);
    

    and then let's say you wish to log events ID from calendar with 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 讨论(0)
  • 2020-11-29 11:09

    Use this code to get daily events,

    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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 11:16

    // Here's a way for you to get all the Calendar events list

    public static void readCalendarEvent(Context context) {
    
           ContentResolver contentResolver = context.getContentResolver();
           cursor = contentResolver.query(Uri.parse("content://com.android.calendar/events"), (new String[] { "_id", "title", "organizer", "dtstart", "dtend"}), null, null, null);
    
           List<GoogleCalendar> gCalendar = new ArrayList<GoogleCalendar>();
            try {
    
                if (cursor.getCount() > 0) {
                    while (cursor.moveToNext()) {
                        GoogleCalendar googleCalendar = new GoogleCalendar();
    
                        int event_ID = cursor.getInt(0);
                        googleCalendar.setEvent_id(event_ID);
                        String title = cursor.getString(1);
                        googleCalendar.setTitle(title);
                        String mOrganizer = cursor.getString(2);
                        googleCalendar.setOrganizer(mOrganizer);
                        String dtStart = cursor.getString(3);
                        googleCalendar.setDtstart(dtStart);
                        String dtEnd = cursor.getString(4);
                        googleCalendar.setDtend(dtEnd);
                        gCalendar.add(googleCalendar);
                        Log.d("CaledarM",googleCalendar.getTitle()+" name = "+googleCalendar.getOrganizer()+" dateStart = "+googleCalendar.getDtstart()+" Size = " + gCalendar.size());
                    }
                }
            } catch (AssertionError ex) {
                ex.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    // class GoogleCalendar()

    public class GoogleCalendar {
    
        private int event_id;
        private String title,
                organizer,
                dtstart,
                dtend;
    
        public GoogleCalendar()
        {
        }
    
        public int getEvent_id() {
            return event_id;
        }
    
        public void setEvent_id(int calendar_id) {
            event_id = calendar_id;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getOrganizer() {
            return organizer;
        }
    
        public void setOrganizer(String description) {
            this.organizer = description;
        }
    
        public String getDtstart() {
            return dtstart;
        }
    
        public void setDtstart(String dtstart1) {
            this.dtstart = dtstart1;
        }
    
        public String getDtend() {
            return dtend;
        }
    
        public void setDtend(String dtend1) {
            this.dtend = dtend1;
        }
    }
    
    0 讨论(0)
提交回复
热议问题