如何从日历中获取所有事件? [英] How to get all the events from calendar?

查看:15
本文介绍了如何从日历中获取所有事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我可以使用此代码获取事件

Hi I am able to get events using this code

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, 
        null);

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

but as you see I can get only events within the given time duration, but I need all the events (no any time specification excluding repeated events) is this possible ? and how ? please help me.

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

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

it gives the error 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,
                        null, 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());
    }
}

你也可以看看这个

从日历中获取事件

这篇关于如何从日历中获取所有事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆