3

我正在尝试在 android 版本 4 中显示日历事件,它可以为此类事件提供标题的数组列表和其他详细信息,但无法显示。
我收到错误,因为找不到日历

public void readCalendar1()
{

    ContentResolver contentResolver = this.getContentResolver();

    // Fetch a list of all calendars synced with the device, their display names and whether the
    // user has them selected for display.

    Cursor cursor;
    cursor = contentResolver.query(CalendarContract.Calendars.CONTENT_URI,  new String[]
            { CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME },
            null, null, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + " ASC");

    HashSet<String> calendarIds1 = new HashSet<String>();

    while (cursor.moveToNext()) 
    {

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

        System.out.println("Id: " + _id + " Display Name: " + displayName );
        calendarIds1.add(_id);
    }

    // For each calendar, display all the events from the previous week to the end of next week.        
    for (String id : calendarIds1)
    {
        Uri builder =  CalendarContract.Events.CONTENT_URI;
        System.out.println("str in read cal1 "+strconvert+"str2 in read cal1 "+strconvert1);

//strconvert and strconvert1 are string which have the particular dates



        Cursor eventCursor = contentResolver.query(builder,
                new String[] { CalendarContract.Events.TITLE, strconvert,
            strconvert1, CalendarContract.Events.ALL_DAY,CalendarContract.Events.EVENT_LOCATION,
            CalendarContract.Events.HAS_ALARM,CalendarContract.Events.DESCRIPTION},
                "calendar_id=" + id,null,"dtstart ASC" ); 
        // For a full list of available columns see http://tinyurl.com/yfbg76w
        int n=eventCursor.getCount();
        System.out.println("No. of rows is="+n);
        while(eventCursor.moveToNext())
        {
            title = eventCursor.getString(0);
            begin = new Date(eventCursor.getLong(1));
            end = new Date(eventCursor.getLong(2));
            allDay = !eventCursor.getString(3).equals("0");
            loc=eventCursor.getString(4);
            hasalarm = !eventCursor.getString(5).equals("0");
            desc=eventCursor.getString(6);

                titlestr.add(title);
                sdatestr.add(begin.toString());
                edatestr.add(end.toString());
                locstr.add(loc);
                descstr.add(desc);
                alarmstr.add(hasalarm.toString());
                System.out.println("Title String: " + titlestr);
                System.out.println("Begin String: " + sdatestr);
                System.out.println("End String: " + edatestr);
                System.out.println("Loc String: " + locstr);
                System.out.println("Desc String: " + descstr);
                System.out.println("Alarm String: " + alarmstr);
//              }

            System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
                    " All Day: " + allDay+" Location="+loc+"  Descriptn="+desc);
        }
    }




}
4

1 回答 1

2

这段代码适用于我阅读 ics android 4 和 ics 以上的两个版本

//for os version android bELOW version 4(ICS)   


public static boolean eventChecker(Context context,ContentResolver cr,String calID){


        Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
        long now = new Date().getTime();
        ContentUris.appendId(builder, now);
        ContentUris.appendId(builder, now + DateUtils.YEAR_IN_MILLIS);





        Cursor eventCursorr = cr.query(builder.build(),
                new String[] { "title", "begin","description"}, "Calendars._id=" + calID,
                null, "startDay ASC, startMinute ASC"); 

        while (eventCursorr.moveToNext()) {
            final String titler = eventCursorr.getString(0).trim();
            final Date beginr = new Date(eventCursorr.getLong(1));
            final String descriptionr = eventCursorr.getString(2).trim();



            SimpleDateFormat sdfrr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String stimesr = sdfrr.format(beginr);


     System.out.println("titler "+titler +"stimesr "+stimesr +"descriptionr "+descriptionr );






        }





    }

//for os version android version 4(ICS) AND ABOVE   

    @TargetApi(14)
    public static boolean eventChecker14(Context context,ContentResolver contentResolver,String calID){



         Uri builder =  CalendarContract.Events.CONTENT_URI;


            String[] COLS = new String[]{ CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART,CalendarContract.Events.DESCRIPTION};


            Cursor eventCursor = contentResolver.query(builder,
                    COLS,
                    "calendar_id=" + calID,null,"dtstart ASC" ); 
            int n=eventCursor.getCount();

            System.out.println("No. of rows is="+n);

            while(eventCursor.moveToNext())
            {
                String title1 = eventCursor.getString(0).trim();
                Date begin1 = new Date(eventCursor.getLong(1));
                String desc1=eventCursor.getString(2).trim();

                SimpleDateFormat sdfrr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                String stimesr = sdfrr.format(begin1);




                System.out.println("title1"+title1+"desc1"+desc1+"stimesr"+stimesr);


            }



    }
于 2012-12-11T12:37:31.667 回答