android – 如何从日历中获取所有事件?

前端之家收集整理的这篇文章主要介绍了android – 如何从日历中获取所有事件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨,我可以使用此代码获取事件
cur = null;
    cr = getContentResolver();


    // Construct the query with the desired date range.
    Uri.Builder builder = Instances.CONTENT_URI.buildUpon();

    ContentUris.appendId(builder,0);
    ContentUris.appendId(builder,endMillis);


    // Submit the query
    cur =  cr.query(builder.build(),INSTANCE_PROJECTION,null,null);

但是如你所见,我只能在给定的持续时间内获得事件,但我需要所有事件(没有任何时间规范,不包括重复事件)这可能吗?如何 ?
请帮我.

cur =  cr.query(Uri.parse("content://com.android.calendar/events"),null);

它给出了错误java.lang.IllegalArgumentException:

解决方法

试试这个代码……
public class Utility {

    public static ArrayList<String> nameOfEvent = new ArrayList<String>();
    public static ArrayList<String> startDates = new ArrayList<String>();
    public static ArrayList<String> endDates = new ArrayList<String>();
    public static ArrayList<String> descriptions = new ArrayList<String>();

    public static ArrayList<String> readCalendarEvent(Context context) {
        Cursor cursor = context.getContentResolver()
                .query(
                        Uri.parse("content://com.android.calendar/events"),new String[] { "calendar_id","title","description","dtstart","dtend","eventLocation" },null);
        cursor.moveToFirst();
        // fetching calendars name
        String CNames[] = new String[cursor.getCount()];

        // fetching calendars id
        nameOfEvent.clear();
        startDates.clear();
        endDates.clear();
        descriptions.clear();
        for (int i = 0; i < CNames.length; i++) {

            nameOfEvent.add(cursor.getString(1));
            startDates.add(getDate(Long.parseLong(cursor.getString(3))));
            endDates.add(getDate(Long.parseLong(cursor.getString(4))));
            descriptions.add(cursor.getString(2));
            CNames[i] = cursor.getString(1);
            cursor.moveToNext();

        }
        return nameOfEvent;
    }

    public static String getDate(long milliSeconds) {
        SimpleDateFormat formatter = new SimpleDateFormat(
                "dd/MM/yyyy hh:mm:ss a");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(milliSeconds);
        return formatter.format(calendar.getTime());
    }
}

你也可以检查一下

Getting events from calendar

猜你在找的Android相关文章