如何在Android设备中获取设备日历事件列表?

时间:2012-04-09 05:32:06

标签: android events calendar

我想在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。

3 个答案:

答案 0 :(得分:35)

我的问题已解决,我在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());
    }

答案 1 :(得分:1)

非常感谢! 但是,您只从一个日历中获得了所有活动,例如您的主要Google帐户,如何从所有可用日历中获取活动?

请注意,在内置日历窗口小部件中,您可以查看来自Gmail,Exchange,Facebook等的事件。 如何获得这些活动?

至于日期或星期的特定事件,我是通过使用 在特定日期,查询语句中的选择子句和选择Args如下:

这些是选择和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);

答案 2 :(得分:0)

这是获取所有日历事件列表的一种方法。

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();
                    // event_ID: ID of tabel Event 
                    int event_ID = cursor.getInt(0);
                    googleCalendar.setEvent_id(event_ID);
                    // title of Event
                    String title = cursor.getString(1);
                    googleCalendar.setTitle(title);
                    String mOrganizer = cursor.getString(2);
                    googleCalendar.setOrganizer(mOrganizer);
                    // Date start of Event
                    String dtStart = cursor.getString(3);
                    googleCalendar.setDtstart(dtStart);
                    // Date end of Event
                    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;
    }
}