Monday 21 March 2016

Method to add events from your Android App to default Calendar application



Please pass required parameters to the below method in order add events to default Calendar in android . 

Add Calendar and Accounts permission to manifest. And Also ask for Calendar Runtime permission for 6.0.

 Accounts permission is normal permission so Runtime implementation for same is not required.

 1. Manifest File

<uses-permission android:name="android.permission.GET_ACCOUNTS"/> <uses-permission android:name="android.permission.READ_CALENDAR"/> <uses-permission android:name="android.permission.WRITE_CALENDAR"/>
2. Methods to add event to Calendar (Modify as per need)

//here you can pass a model class which will contains all your values instead of many parameters

private void addEventToCalendar(Activity activity,String title,String startDateStr,String startTime, String endDateStr,String endTime,String eventDescription) { //Getting google accounts linked here Account[] accounts = AccountManager.get(activity).getAccountsByType( "com.google"); String accountName=""; for (Account acc : accounts) { Log.d(">>>>>>>", "Name: " + acc.name + "-----Type: " + acc.type); accountName = acc.name; } //Getting required calendar ID here long calID = getCalendarId(activity,accountName); long startMillis = 0; long endMillis = 0; Date startDate = null; //Converting date/time as per need try { startDate = new SimpleDateFormat("yyyy-MM-dd hh:mmaa", Locale.ENGLISH).parse(startDateStr+" "+startTime); } catch (ParseException e1) { e1.printStackTrace(); } startMillis = startDate.getTime(); Date endDate = null; try { endDate = new SimpleDateFormat("yyyy-MM-dd hh:mmaa", Locale.ENGLISH).parse(endDateStr+" "+endTime); } catch (ParseException e) { e.printStackTrace(); } endMillis = endDate.getTime(); //Creating content values which will be used to enter data from a query into calendar content resolver ContentResolver cr = activity.getContentResolver(); ContentValues values = new ContentValues(); values.put(CalendarContract.Events.DTSTART, startMillis); values.put(CalendarContract.Events.DTEND, endMillis); values.put(CalendarContract.Events.TITLE, title); values.put(CalendarContract.Events.DESCRIPTION,eventDescription); values.put(CalendarContract.Events.CALENDAR_ID, calID); values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance().getTimeZone().getDisplayName()); try { // Executing query to enter event into the calendar here Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values); } catch(Exception e) { e.printStackTrace(); } } private static long getCalendarId(Activity activity,String MY_ACCOUNT_NAME) { String[] projection = new String[] { BaseColumns._ID }; String selection = CalendarContract.Calendars.ACCOUNT_NAME + " = ?"; String[] selArgs = new String[] { MY_ACCOUNT_NAME }; Cursor cursor = activity.getContentResolver().query( CalendarContract.Calendars.CONTENT_URI, projection, selection, selArgs, null); if (cursor.moveToFirst()) { return cursor.getLong(0); } return -1; }


References : http://developer.android.com/index.html