This example explains how to call and use system calendar into your application.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it CalendarApplication.
2.) Write following into main.xml file:
<LinearLayout xmlns:android=<em>"http://schemas.android.com/apk/res/android"</em>
android:layout_width=<em>"fill_parent"</em>
android:layout_height=<em>"fill_parent"</em>
android:orientation=<em>"vertical"</em> >
<Button
android:id=<em>"@+id/button1"</em>
android:layout_width=<em>"wrap_content"</em>
android:layout_height=<em>"wrap_content"</em>
android:onClick=<em>"onClick"</em>
android:text=<em>"Create Event"</em> />
<Button
android:id=<em>"@+id/button2"</em>
android:layout_width=<em>"wrap_content"</em>
android:layout_height=<em>"wrap_content"</em>
android:onClick=<em>"queryCalendar"</em>
android:text=<em>"Query Calendar"</em> />
</LinearLayout>
3.) Put INTERNET permission to manifest file:
<uses-permission
android:name=“android.permission.READ_CALENDAR”>
</uses-permission>
<uses-permission
android:name=“android.permission.WRITE_CALENDAR”>
</uses-permission>
4.) Run for output.
Steps:
1.) Create a project named CalendarApplication and set the information as stated in the image.
Build Target: Android 4.0
Application Name: CalendarApplication
Package Name: com. example. CalendarApplication
Activity Name: CalendarApplicationActivity
Min SDK Version: 14
2.) Open CalendarApplicationActivity.java file and write following code there:
<strong>import</strong> java.util.GregorianCalendar;
<strong>import</strong> android.app.Activity;
<strong>import</strong> android.content.ContentResolver;
<strong>import</strong> android.content.Intent;
<strong>import</strong> android.database.Cursor;
<strong>import</strong> android.net.Uri;
<strong>import</strong> android.os.Bundle;
<strong>import</strong> android.provider.CalendarContract;
<strong>import</strong> android.provider.CalendarContract.Calendars;
<strong>import</strong> android.provider.CalendarContract.Events;
<strong>import</strong> android.view.View;
<strong>import</strong> android.widget.Toast;
<strong>public</strong> <strong>class</strong> CalendarApplicationActivity <strong>extends</strong> Activity {
<strong>public</strong> <strong>static</strong> <strong>final</strong> String[] <em>EVENT_PROJECTION</em> = <strong>new</strong> String[] {
Calendars.<em>_ID</em>, // 0
Calendars.<em>ACCOUNT_NAME</em>, // 1
Calendars.<em>CALENDAR_DISPLAY_NAME</em> // 2
};
<strong>private</strong> <strong>static</strong> <strong>final</strong> <strong>int</strong> <em>PROJECTION_DISPLAY_NAME_INDEX</em> = 2;
@Override
<strong>public</strong> <strong>void</strong> onCreate(Bundle savedInstanceState) {
<strong>super</strong>.onCreate(savedInstanceState);
setContentView(R.layout.<em>main</em>);
}
<strong>public</strong> <strong>void</strong> onClick(View view) {
Intent intent = <strong>new</strong> Intent(Intent.<em>ACTION_INSERT</em>);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra(Events.<em>TITLE</em>, "Learn Android");
intent.putExtra(Events.<em>EVENT_LOCATION</em>, "Home suit home");
intent.putExtra(Events.<em>DESCRIPTION</em>, "Download Examples");
GregorianCalendar calDate = <strong>new</strong> GregorianCalendar(2012, 10, 02);
intent.putExtra(CalendarContract.<em>EXTRA_EVENT_BEGIN_TIME</em>,
calDate.getTimeInMillis());
intent.putExtra(CalendarContract.<em>EXTRA_EVENT_END_TIME</em>,
calDate.getTimeInMillis());
intent.putExtra(CalendarContract.<em>EXTRA_EVENT_ALL_DAY</em>, <strong>true</strong>);
intent.putExtra(Events.<em>RRULE</em>,
"FREQ=WEEKLY;COUNT=11;WKST=SU;BYDAY=TU,TH");
intent.putExtra(Events.<em>ACCESS_LEVEL</em>, Events.<em>ACCESS_PRIVATE</em>);
intent.putExtra(Events.<em>AVAILABILITY</em>, Events.<em>AVAILABILITY_BUSY</em>);
startActivity(intent);
}
<strong>public</strong> <strong>void</strong> queryCalendar(View view) {
Cursor cur = <strong>null</strong>;
ContentResolver cr = getContentResolver();
Uri uri = Calendars.<em>CONTENT_URI</em>;
String selection = "((" + Calendars.<em>ACCOUNT_NAME</em> + " = ?) AND ("
+ Calendars.<em>ACCOUNT_TYPE</em> + " = ?))";
String[] selectionArgs = <strong>new</strong> String[] { "Lars.Vogel@gmail.com",
"com.google" };
cur = cr.query(uri, <em>EVENT_PROJECTION</em>, selection, selectionArgs, <strong>null</strong>);
<strong>while</strong> (cur.moveToNext()) {
String displayName = <strong>null</strong>;
displayName = cur.getString(<em>PROJECTION_DISPLAY_NAME_INDEX</em>);
Toast.<em>makeText</em>(<strong>this</strong>, "Calendar " + displayName, Toast.<em>LENGTH_SHORT</em>)
.show();
}
}
}
3.) Compile and build the project.
Output
4.) If your phone doesn’t have any default calendar installed you will see the screen as below. Follow the instructions and see your calendar.











