Android在平板电脑中获取日历事件

时间:2011-12-04 07:33:49

标签: android calendar

我使用下面的代码在我的应用程序中获取日历事件。它在Android手机中运行良好但是当我在Android平板电脑上试用这个代码时,我的应用程序崩溃了。所以我不知道究竟是什么问题,以及为什么它不适用于平板电脑。

public void syncCalander() {
    try {

        nameValues = new ArrayList<NameValuePair>();
        StringBuffer calbuffers;
        int cnt = 1;
        StringBuffer calbufferimeis = new StringBuffer();

        ContentResolver contentResolver = getApplicationContext()
        .getContentResolver();
        final Cursor cursor = contentResolver.query(
            Uri.parse("content://com.android.calendar/calendars"),
            (new String[] { "_id", "displayName", "selected" }), null,
            null, null);
        if (cursor.getCount() == 0) {

        } else {

            HashSet<String> calendarIds = new HashSet<String>();
            CalendarModel calModel = new CalendarModel();

            CalendarModel.CALENDERLIST.add(calModel);
            int val = cursor.getCount();
            Log.i("=============total event============>", "." + val);
            while (cursor.moveToNext()) {

                final String _id = cursor.getString(0);
                final String displayName = cursor.getString(1);
                final Boolean selected = !cursor.getString(2).equals("0");
                calModel.setCalendarEvent(displayName);
                CalendarModel.CALENDERLIST.add(calModel);
                Log.i("--------Display Name----------", "" + "Id: " + _id
                    + " Display Name: " + displayName + " Selected: "
                    + selected);
                calendarIds.add(_id);
                Log.i("============celenderIDs==========>", "."
                    + calendarIds);
            }

            for (String id : calendarIds) {

                Uri.Builder builder = Uri.parse(
                    "content://com.android.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");
                Log.i("============cursor size===========>", "."
                    + eventCursor.getCount());

                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");
                    calModel.setCalendarDate(begin.toString());
                    CalendarModel.CALENDERLIST.add(calModel);
                    Log.i("-----Title--------", "Title: " + title
                        + " Begin: " + begin + " End: " + end
                        + " All Day: " + allDay);
                    SimpleDateFormat formatter = new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm:ss");
                    String date = formatter.format(begin);

                    int callength = CalendarModel.CALENDERLIST.size();

                    calbuffers = new StringBuffer();
                    calbuffers.append("{\"Calenderevent\":\"" + title
                        + "\"," + "\"Calenderdate\":\"" + date + "\"}");

                    calbuffers.append(",");
                    calbufferimeis.append(calbuffers);

                }
                eventCursor.close();
            }

        }
        cursor.close();
    } catch (Exception e) {
    }

}

1 个答案:

答案 0 :(得分:0)

我使用

content://calendar/calendars

而不是

content://com.android.calendar/calendars

用于Android平板电脑应用程序