java - 如何使用 android 4.0 日历提供程序 API 选择要从中提取事件的日历?

标签 java android android-contentprovider android-calendar

我一直在编写一种闹钟应用程序,暂时仅供我自己使用,但如果有人需要,我可能会在完成后免费提供它。无论如何,我都被困住了。我想要从日历中得到的只是用户日历中的下一个事件,这样我就可以显示标题、时间、截止时间和位置。

这是我到目前为止所得到的:

    //get cursor to first row of table of events
        mCursor = getContentResolver().query(
        CalendarContract.Events.CONTENT_URI, COLS, null, null, null);
        mCursor.moveToFirst();

        //variables; title, startdate in miliseconds, etc
        String nextAppointmentTitle = "N/A";
        Long start = 0L;            //tf(start) for appointmentTime
        String timeUntilNextAppointment = "N/A";
        String nextAppointmentLocation = "N/A";


        Format df = DateFormat.getDateFormat(this);
        Format tf = DateFormat.getTimeFormat(this);
        try {
        nextAppointmentTitle = mCursor.getString(0);
        start = mCursor.getLong(1);
        } catch (Exception e) {
        //ignore for the time being
        }


        //create a date object for the time of the event
        GregorianCalendar appointmentTime = (GregorianCalendar) GregorianCalendar.getInstance();
        appointmentTime.setTimeInMillis(start);
        //create date object for current time       
        GregorianCalendar now = (GregorianCalendar) GregorianCalendar.getInstance();

        //get the time from current time until start of event
        long millisUntilNextMeeting = (long) appointmentTime.getTimeInMillis() - (long) now.getTimeInMillis();
        GregorianCalendar timeUntilNextMeeting = (GregorianCalendar) GregorianCalendar.getInstance();
        timeUntilNextMeeting.clear();
        timeUntilNextMeeting.setTime(new Date(millisUntilNextMeeting));

        millisUntilNextMeeting= (millisUntilNextMeeting/1000) /60;

        if (timeUntilNextMeeting.get(GregorianCalendar.HOUR_OF_DAY) > 0) {
            int hours = timeUntilNextMeeting.get(GregorianCalendar.HOUR_OF_DAY) + 6;
            timeUntilNextAppointment = "" + hours + " hours";
        } else {
            timeUntilNextAppointment = "" + timeUntilNextMeeting.get(GregorianCalendar.MINUTE) + " minutes";
        } 
// ... do things with values

它不是显示我的下一个约会,而是显示艾伯塔省家庭日,这显然是在二月举行的。我环顾了一下我的日历,似乎它任意选择了我的“加拿大假期”日历,谷歌非常有帮助地将其添加到我的手机中,而不是我实际放入的日历。我似乎不知道如何选择正确的日历,而不必每次都让用户选择它。

有人知道我如何从我想要的日历中获取我想要的值吗?我真的很感激您的帮助。

PS 我知道这个应用程序无法在 4.0 之前的设备上运行,但我并不在乎再花 4 个小时来尝试破译可怕的 google api 文档。

最佳答案

首先使用 CalendarContract.Calendars 中的常量找到所需的日历。对 NAME 或 CALENDAR_DISPLAY_NAME 进行查询。从返回的行中获取 _ID 值。使用此值查询 CalendarContract.Events.CALENDAR_ID。这将为您提供由 CalendarContract.Events.CALENDAR_ID 标识的日历的所有事件。

您所做的是获取所有日历的所有事件。

关于java - 如何使用 android 4.0 日历提供程序 API 选择要从中提取事件的日历?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13099827/

相关文章:

java - 从数据库中获取最高 10 个值并显示在 RecyclerView 中

java - 迭代同步包装器是否安全?

android - WindowManager.LayoutParams 为什么有setTitle 方法?

android - 使用 Intent.ACTION_GET_CONTENT 时如何避免 "SecurityException"

java - JSON 对象无法获取字符串

java - AlertDialog 错误字符串资源 ID #0x1

android - 弹出窗口内的微调器

android - 如何在android中将路径绘制到 View 中

android - 如何从 CursorLoader 刷新光标?

android - 如何在 Android 中使用带有 DiskLruCache 的 ContentProvider