I'm doing a project where I want to invoke the Android calender and also set an alarm. The开发者_开发技巧 user enters the date and time in the application itself. Is there any way we can pass this date and time to the calendar application while opening it?
To add an entry to a specific calendar, we need to configure a calendar entry to insert using the ContentValues as follows:
ContentValues event = new ContentValues();
Each event needs to be tied to a specific Calendar, so the first thing you're going to want to set is the identifier of the Calendar to insert this event into:
event.put("calendar_id", calId);
We then set some of the basic information about the event, including String fields such as the event title, description and location.
event.put("title", "Event Title");
event.put("description", "Event Desc");
event.put("eventLocation", "Event Location");
There are a number of different options for configuring the time and date of an event.
We can set the event start and end information as follows:
long startTime = START_TIME_MS;
long endTime = END_TIME_MS;
event.put("dtstart", startTime);
event.put("dtend", endTime);
If we are adding a birthday or holiday, we would set the entry to be an all day event:
event.put("allDay", 1); // 0 for false, 1 for true
This information is sufficient for most entries. However, there are a number of other useful calendar entry attributes.
For example, you can set the event status to tentative (0), confirmed (1) or canceled (2):
event.put("eventStatus", 1);
You can control who can see this event by setting its visibility to default (0), confidential (1), private (2), or public (3):
event.put("visibility", 0);
You can control whether an event consumes time (can have schedule conflicts) on the calendar by setting its transparency to opaque (0) or transparent (1).
event.put("transparency", 0);
You can control whether an event triggers a reminder alarm as follows:
event.put("hasAlarm", 1); // 0 for false, 1 for true
Once the calendar event is configured correctly, we're ready to use the ContentResolver to insert the new calendar entry into the appropriate Uri for calendar events:
Uri eventsUri = Uri.parse("content://calendar/events");
Uri url = getContentResolver().insert(eventsUri, event);
精彩评论