简体   繁体   中英

Get calendar events for ICS android version 4 for a particular date

I m tring to display the calendar events in android version 4 which can give the arraylist for the title and other details for the events like this but unable to display.
I get the error as calendar not found .

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);
        }
    }




}

This code is working for me to read for both versions below ics android 4 and above 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);


            }



    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM