【问题标题】:How do I go about selecting which calendar to pull events from using the android 4.0 calendar provider api?如何使用 android 4.0 日历提供程序 api 选择要从哪个日历中提取事件?
【发布时间】:2014-09-21 13:22:46
【问题描述】:

我一直在编写一种警报应用程序,暂时只是为了我自己,但如果有人在完成后想要它,我可能会免费提供它。无论如何,我一直受阻。我想要从日历中获取的只是用户日历中的下一个事件,这样我就可以显示标题、时间、截止时间和位置。

这是我目前所得到的:

    //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

显示的不是我的下一个约会,而是显示阿尔伯塔家庭日,这显然发生在 2 月。我用我的日历环顾四周,似乎它随意选择了我的“加拿大假期”日历,谷歌非常有帮助地添加到我的手机中,而不是我实际放入的日历。我似乎无法弄清楚如何选择正确的日历,而无需用户每次都选择它。

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

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

【问题讨论】:

    标签: java android android-contentprovider android-calendar


    【解决方案1】:

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

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

    【讨论】:

    • 哦,谢谢。那讲得通。实际上,尽管这样可能是最好的。使我不必为用户提供某种方式来选择他们想要的。你知道我能做些什么来让事件尽快发生吗?
    • 嘿@Joe 我这样做是为了获得最近的约会:'while(mCursor.moveToNext()) { if( now.getTimeInMillis()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多