简体   繁体   English

如何在Android默认日历中显示添加的事件

[英]How to show added events in Android default Calendar

This is the code that I have used to add Calendar events, it's working fine. 这是我用来添加Calendar事件的代码,它工作正常。 But I can't see the events that I have added. 但是我看不到我添加的事件。 The code doesn't have any errors. 该代码没有任何错误。

CalendarView cal = (CalendarView) rootView.findViewById(R.id.calendarView1);
//Context ctx;
ContentResolver cr = getActivity().getContentResolver();
ContentValues event = new ContentValues();
event.put("calendar_id", R.id.calendarView1);
event.put("title", "WOOT");
event.put("description", "Wootification");
event.put("eventLocation", "Wootness");
event.put("allDay", 0); 
event.put("eventStatus", 1);
//event.put("visibility", 0);
//event.put("transparency", 0);
// event.put(Events.VISIBLE, 1);
event.put("hasAlarm", 0);

Calendar start = Calendar.getInstance();
start.set(2015, 4, 27, 8, 0, 0);

Calendar end = Calendar.getInstance();
end.set(2015, 4, 30, 9, 0, 0);

long startTime = start.getTimeInMillis();       
// startTime = startTime * 1000;

long endTime = end.getTimeInMillis();
// endTime = endTime * 1000;
TimeZone timeZone = TimeZone.getDefault();
event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
event.put("dtstart", startTime);
event.put("dtend", endTime);
System.out.println(startTime);
System.out.println(endTime);
// insert event to calendar
Uri eventsUri = Uri.parse(CalendarContract.Events.CONTENT_URI.toString());
cr.insert(eventsUri, event);
System.out.println(event);
// add 10 minute reminder for the event
ContentValues reminders = new ContentValues();
reminders.put(Reminders.EVENT_ID, R.id.calendarView1);
reminders.put(Reminders.METHOD, Reminders.METHOD_ALERT);
reminders.put(Reminders.MINUTES, 1);

Uri eventsUrir = Uri.parse("content://com.android.calendar/reminders");
cr.insert(eventsUrir, reminders);

Can someone help me to fix this problem? 有人可以帮我解决这个问题吗?

Just in case if you have not got any solution. 以防万一您没有任何解决方案。

Use following method to add, Remove and delete Event from default Calendar. 使用以下方法从默认日历中添加,删除和删除事件。

public void addEvent(Context context) {
        GregorianCalendar calDate = new GregorianCalendar(this._year, this._month, this._day, this._hour, this._minute);

        try {
            ContentResolver cr = context.getContentResolver();
            ContentValues values = new ContentValues();
            values.put(CalendarContract.Events.DTSTART, calDate.getTimeInMillis());
            values.put(CalendarContract.Events.DTEND, calDate.getTimeInMillis()+60*60*1000);
            values.put(CalendarContract.Events.TITLE, this._title);
            values.put(CalendarContract.Events.CALENDAR_ID, 1);
            values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance()
                    .getTimeZone().getID());
            System.out.println(Calendar.getInstance().getTimeZone().getID());
            Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

            // Save the eventId into the Task object for possible future delete.
            this._eventId = Long.parseLong(uri.getLastPathSegment());
            // Add a 5 minute, 1 hour (2 reminders)
            setReminder(cr, this._eventId, 5);
            setReminder(cr, this._eventId, 60);    
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
// routine to add reminders with the event
public void setReminder(ContentResolver cr, long eventID, int timeBefore) {
    try {
        ContentValues values = new ContentValues();
        values.put(CalendarContract.Reminders.MINUTES, timeBefore);
        values.put(CalendarContract.Reminders.EVENT_ID, eventID);
        values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
        Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
        Cursor c = CalendarContract.Reminders.query(cr, eventID,
                new String[]{CalendarContract.Reminders.MINUTES});
        if (c.moveToFirst()) {
            System.out.println("calendar"
                    + c.getInt(c.getColumnIndex(CalendarContract.Reminders.MINUTES)));
        }
        c.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//use following method to remove an event from the calendar using the eventId stored within the Task object.
public void removeEvent(Context context) {
    ContentResolver cr = context.getContentResolver();

    int iNumRowsDeleted = 0;

    Uri eventsUri = Uri.parse(CALENDAR_URI_BASE+"events");
    Uri eventUri = ContentUris.withAppendedId(eventsUri, this._eventId);
    iNumRowsDeleted = cr.delete(eventUri, null, null);

    Log.i(DEBUG_TAG, "Deleted " + iNumRowsDeleted + " calendar entry.");
}


public int updateEvent(Context context) {
    int iNumRowsUpdated = 0;
    GregorianCalendar calDate = new GregorianCalendar(this._year, this._month, this._day, this._hour, this._minute);

    ContentValues event = new ContentValues();

    event.put(CalendarContract.Events.TITLE, this._title);
    event.put("hasAlarm", 1); // 0 for false, 1 for true
    event.put(CalendarContract.Events.DTSTART, calDate.getTimeInMillis());
    event.put(CalendarContract.Events.DTEND, calDate.getTimeInMillis()+60*60*1000);

    Uri eventsUri = Uri.parse(CALENDAR_URI_BASE+"events");
    Uri eventUri = ContentUris.withAppendedId(eventsUri, this._eventId);

    iNumRowsUpdated = context.getContentResolver().update(eventUri, event, null,
            null);

    // TODO put text into strings.xml
    Log.i(DEBUG_TAG, "Updated " + iNumRowsUpdated + " calendar entry.");

    return iNumRowsUpdated;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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