0

好的,所以我正在制作一个从手机中提取 synced 日历并显示它们的应用程序,我已经阅读了许多教程,但我仍然不确定如何做到这一点。我想提取一个带有ACCOUNT_TYPE “ca.ryerson”的日历(它由用于教育的谷歌应用程序提供支持),然后我想从该特定数据库中获取所有事件,并且仅在有人点击提供的日期时显示它们。

因此,当有人点击 9 月 5 日时,它会显示来自 ca.ryerson 帐户的 9 月 5 日的所有事件,我对 android 开发相当陌生,并且在数据库处理方面几乎没有经验,我真的很想得到你的帮助。我已经阅读了许多教程并通读了文档,但我仍然无法弄清楚如何做到这一点。

谢谢 :)

4

1 回答 1

1

我能够确定如何做到这一点这是我的代码

ContentResolver contentResolver = getContentResolver();
        final Cursor cursor = contentResolver.query(CalendarContract.Calendars.CONTENT_URI,
                (new String[]{CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME}), null, null, null);

        while (cursor.moveToNext()) {
            final String _id = cursor.getString(0);
            final String displayName = cursor.getString(1);


            int stringLength = displayName.length();
            String output = displayName.substring(stringLength - 10);
            //Log.d("Cursor", output);

            if (output.equals("ryerson.ca")) {
                //Log.d("Cursor", "true");
                calendarID = new String[]{_id};
            }

            Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon();

            Calendar beginTime = Calendar.getInstance();
            beginTime.set(2014, Calendar.SEPTEMBER, 2, 8, 0);
            long startMills = beginTime.getTimeInMillis();

            Calendar endTime = Calendar.getInstance();
            endTime.set(2014, Calendar.SEPTEMBER, 2, 20, 0);
            long endMills = endTime.getTimeInMillis();

            ContentUris.appendId(builder, startMills);
            ContentUris.appendId(builder, endMills);

            Cursor eventCursor = contentResolver.query(builder.build(), new String[]{CalendarContract.Instances.TITLE,
                            CalendarContract.Instances.BEGIN, CalendarContract.Instances.END, CalendarContract.Instances.DESCRIPTION},
                    CalendarContract.Instances.CALENDAR_ID + " = ?", calendarID, null);

            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 String description = eventCursor.getString(3);

                Log.d("Cursor", "Title: " + title + "\tDescription: " + description + "\tBegin: " + begin + "\tEnd: " + end);
            }
于 2014-08-13T21:24:14.343 回答