如何使用ContentValues为Android 4.4 kitkat版本添加和验证日历事件

时间:2014-02-26 06:13:43

标签: android events calendar android-4.4-kitkat

我一直在使用以下代码在Android中添加日历事件,但它不能在Kitkat版本中运行

public class SetEvent {
    Calendar cal = Calendar.getInstance();

    public void SetCalenderEvent(Activity mActivity,
            SetgetCalenderDetails mSetgetCalenderDetails) {
        long eventID;
        Uri EVENTS_URI = Uri.parse(getCalendarUriBase(mActivity) + "events");
        ContentResolver cr = mActivity.getContentResolver();

        ContentValues values = new ContentValues();
        // values.put("event_id",EventID);
        values.put("calendar_id", 1);
        values.put("title", mSetgetCalenderDetails.getName());
        values.put("dtstart",
                CommonMethods.SetEndTime(mSetgetCalenderDetails.getRaceDate())); // event

        values.put("dtend",
                CommonMethods.SetEndTime(mSetgetCalenderDetails.getRaceDate()));

        values.put("description", "Test");
        values.put("eventTimezone", TimeZone.getDefault().getID());
        values.put("eventLocation", mSetgetCalenderDetails.getCircuit() + " "
                + mSetgetCalenderDetails.getCountry());
        values.put("hasAlarm", 1);
        Uri event = null;
        try {
            event = cr.insert(EVENTS_URI, values);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // reminder insert
        Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(mActivity)
                + "reminders");
        values = new ContentValues();
        eventID = Long.parseLong(event.getLastPathSegment());
        values.put("event_id", eventID);
        values.put("method", 1);
        values.put("minutes", 10);
        cr.insert(REMINDERS_URI, values);
    }

    @SuppressWarnings("deprecation")
    private String getCalendarUriBase(Activity act) {

        String calendarUriBase = null;
        Uri calendars = Uri.parse("content://calendar/calendars");
        Cursor managedCursor = null;
        try {
            managedCursor = act.managedQuery(calendars, null, null, null, null);
        } catch (Exception e) {
        }
        if (managedCursor != null) {
            calendarUriBase = "content://calendar/";
        } else {
            calendars = Uri.parse("content://com.android.calendar/calendars");
            try {
                managedCursor = act.managedQuery(calendars, null, null, null,
                        null);
            } catch (Exception e) {
            }
            if (managedCursor != null) {
                calendarUriBase = "content://com.android.calendar/";
            }
        }
        return calendarUriBase;
    }

}

请提前谢谢。

0 个答案:

没有答案