Android - Creating Intents: Calendar
- Feb 2, 2018
- 3 min read
Hello all,
I really enjoyed the Udacity Android Basics by Google lesson Practice with Intents so I am taking some time to fully explore each of the intents in the Android Common Intents documentation. The one I have most recently looked at is Calendar.
First I just created a simple button to activate the intent which looks like this:

And the code in activity_main.xml looks like this:
<Button
android:id="@+id/create_event"
android:layout_width="@dimen/button_width"
android:layout_height="@dimen/button_height"
android:layout_gravity="center"
android:layout_margin="@dimen/margin"
android:onClick="createCalendar"
android:text="Calendar"/>
Then I created an activity in MainActivity.java to call the intent when the button is clicked. To launch a calendar app all you need to do is include this code.
public void createCalendar(View view) {
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(CalendarContract.Events.CONTENT_URI);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Notice that the line:
.setData(CalendarContract.Events.CONTENT_URI);
is necessary in order to launch the calendar app from your own app. Also notice that this differs from the Calendar guidance given. For me, the suggested code “Events.CONTENT_URI” would not work but eventually Android Studio suggested that I change it to “CalendarContract.Events.CONTENT_URI”. Since that change there has been no problem. I have also found that if I delete “CalendarContract.Events”, leaving just “CONTENT_URI” in the perens, then the following dialogue box shows up if I hover over the, now red, “CONTENT_URI”:

Then, if I press ALT+ENTER as instructed this list comes up:

Now, I do not know which one of the options in this list are the correct one so I have kept this line as:
.setData(CalendarContract.Events.CONTENT_URI);
However, other lines of code in this Activity did not populate a list. This suggests that there was only one option for that particular keyword. That is why in the following example some lines include “CalendarContract.Events.” and some do not.
There are also other lines of code you can add to pre-set certain aspects of the calendar event. I experimented with a number of these and ended up with some code that looks like this, with explanatory comments:
/**
* A method that uses the "ACTION_INSERT" intent to set up a calendar event on an Android phone
*/
public void createCalendar(View view) {
//If you uncomment the following line you intialise the boolean "allDayEvent" to true
//boolean allDayEvent = true;
//The next two lines establish the beginTime of the event by year, month, date, hourOfDay and minute
Calendar beginTime = Calendar.getInstance();
beginTime.set(2018,0,19,18,0);
//The next two lines establish the endTime of the event by year, month, date, hourOfDay and minute
Calendar endTime = Calendar.getInstance();
endTime.set(2018,0,19,20,30);
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(CalendarContract.Events.CONTENT_URI)
//If you uncomment the follwing line and the boolean line above the event is set to all day
//.putExtra(EXTRA_EVENT_ALL_DAY, allDayEvent);
//This line sets the start time of the event using data from beginTime above
.putExtra(EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
//This line sets the finish time of the event using data from endTime above
.putExtra(EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())
//This populates the name section of the calendar event
.putExtra(CalendarContract.Events.TITLE, "Udacity Google Developers Meetup")
//This populates the description section of the calendar event
.putExtra(CalendarContract.Events.DESCRIPTION, "Coding Group")
//This populates the location section of the calendar event
.putExtra(CalendarContract.Events.EVENT_LOCATION, "Code Node")
//This populates the availablility of the event
.putExtra(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_BUSY)
//This makes it so you send emails to attendees within this list
.putExtra(Intent.EXTRA_EMAIL, "rowan@example.com,trevor@example.com");
// This line first checks that your phone has an app that can handle the intent before
// starting so the app doesn't crash if your phone doesn't an an appropriate app
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
All of this should allow you to use the Calendar intent in your app and modify it in a number of ways. You may notice that AVAILABILITY is not in the Calendar documentation page however if you click on the link that says CalendarContract.EventsColumn then you will be able to see even more ways you can modify the Calendar event from your own app.
I welcome any comments or questions about this, especially corrections of anything I can improve. For example, I still do not know why Events alone did not work and why CalendarContract.Events. did.
Links:





























Comments