Getting Events from Calendar

Getting events from calendar

Ok i found the answer of this whole of the concept that how to use the google calendar application integration with the android phone.

code:--

first you set this line which will goes to read the calendar events form the other class form your class which is current is the ApplicationSettings.java .

ReadCalendar.readCalendar(ApplicationSettings.this);

package com.mycalendarevents.android;

import java.util.Date;
import java.util.HashSet;
import java.util.regex.Pattern;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.format.DateUtils;

public class ReadCalendar
{
static Cursor cursor;

public static void readCalendar(Context context) {

ContentResolver contentResolver = context.getContentResolver();

// Fetch a list of all calendars synced with the device, their display names and whether the

cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"),
(new String[] { "_id", "displayName", "selected"}), null, null, null);

HashSet<String> calendarIds = new HashSet<String>();

try
{
System.out.println("Count="+cursor.getCount());
if(cursor.getCount() > 0)
{
System.out.println("the control is just inside of the cursor.count loop");
while (cursor.moveToNext()) {

String _id = cursor.getString(0);
String displayName = cursor.getString(1);
Boolean selected = !cursor.getString(2).equals("0");

System.out.println("Id: " + _id + " Display Name: " + displayName + " Selected: " + selected);
calendarIds.add(_id);
}
}
}
catch(AssertionError ex)
{
ex.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}

// For each calendar, display all the events from the previous week to the end of next week.
for (String id : calendarIds) {
Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
//Uri.Builder builder = Uri.parse("content://com.android.calendar/calendars").buildUpon();
long now = new Date().getTime();

ContentUris.appendId(builder, now - DateUtils.DAY_IN_MILLIS * 10000);
ContentUris.appendId(builder, now + DateUtils.DAY_IN_MILLIS * 10000);

Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + 1,
null, "startDay ASC, startMinute ASC");

System.out.println("eventCursor count="+eventCursor.getCount());
if(eventCursor.getCount()>0)
{

if(eventCursor.moveToFirst())
{
do
{
Object mbeg_date,beg_date,beg_time,end_date,end_time;

final String title = eventCursor.getString(0);
final Date begin = new Date(eventCursor.getLong(1));
final Date end = new Date(eventCursor.getLong(2));
final Boolean allDay = !eventCursor.getString(3).equals("0");

/* System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
" All Day: " + allDay);
*/
System.out.println("Title:"+title);
System.out.println("Begin:"+begin);
System.out.println("End:"+end);
System.out.println("All Day:"+allDay);

/* the calendar control metting-begin events Respose sub-string (starts....hare) */

Pattern p = Pattern.compile(" ");
String[] items = p.split(begin.toString());
String scalendar_metting_beginday,scalendar_metting_beginmonth,scalendar_metting_beginyear,scalendar_metting_begindate,scalendar_metting_begintime,scalendar_metting_begingmt;

scalendar_metting_beginday = items[0];
scalendar_metting_beginmonth = items[1];
scalendar_metting_begindate = items[2];
scalendar_metting_begintime = items[3];
scalendar_metting_begingmt = items[4];
scalendar_metting_beginyear = items[5];

String calendar_metting_beginday = scalendar_metting_beginday;
String calendar_metting_beginmonth = scalendar_metting_beginmonth.toString().trim();

int calendar_metting_begindate = Integer.parseInt(scalendar_metting_begindate.trim());

String calendar_metting_begintime = scalendar_metting_begintime.toString().trim();
String calendar_metting_begingmt = scalendar_metting_begingmt;
int calendar_metting_beginyear = Integer.parseInt(scalendar_metting_beginyear.trim());

System.out.println("calendar_metting_beginday="+calendar_metting_beginday);

System.out.println("calendar_metting_beginmonth ="+calendar_metting_beginmonth);

System.out.println("calendar_metting_begindate ="+calendar_metting_begindate);

System.out.println("calendar_metting_begintime="+calendar_metting_begintime);

System.out.println("calendar_metting_begingmt ="+calendar_metting_begingmt);

System.out.println("calendar_metting_beginyear ="+calendar_metting_beginyear);

/* the calendar control metting-begin events Respose sub-string (starts....ends) */

/* the calendar control metting-end events Respose sub-string (starts....hare) */

Pattern p1 = Pattern.compile(" ");
String[] enditems = p.split(end.toString());
String scalendar_metting_endday,scalendar_metting_endmonth,scalendar_metting_endyear,scalendar_metting_enddate,scalendar_metting_endtime,scalendar_metting_endgmt;

scalendar_metting_endday = enditems[0];
scalendar_metting_endmonth = enditems[1];
scalendar_metting_enddate = enditems[2];
scalendar_metting_endtime = enditems[3];
scalendar_metting_endgmt = enditems[4];
scalendar_metting_endyear = enditems[5];

String calendar_metting_endday = scalendar_metting_endday;
String calendar_metting_endmonth = scalendar_metting_endmonth.toString().trim();

int calendar_metting_enddate = Integer.parseInt(scalendar_metting_enddate.trim());

String calendar_metting_endtime = scalendar_metting_endtime.toString().trim();
String calendar_metting_endgmt = scalendar_metting_endgmt;
int calendar_metting_endyear = Integer.parseInt(scalendar_metting_endyear.trim());

System.out.println("calendar_metting_beginday="+calendar_metting_endday);

System.out.println("calendar_metting_beginmonth ="+calendar_metting_endmonth);

System.out.println("calendar_metting_begindate ="+calendar_metting_enddate);

System.out.println("calendar_metting_begintime="+calendar_metting_endtime);

System.out.println("calendar_metting_begingmt ="+calendar_metting_endgmt);

System.out.println("calendar_metting_beginyear ="+calendar_metting_endyear);

/* the calendar control metting-end events Respose sub-string (starts....ends) */

System.out.println("only date begin of events="+begin.getDate());
System.out.println("only begin time of events="+begin.getHours() + ":" +begin.getMinutes() + ":" +begin.getSeconds());

System.out.println("only date begin of events="+end.getDate());
System.out.println("only begin time of events="+end.getHours() + ":" +end.getMinutes() + ":" +end.getSeconds());

beg_date = begin.getDate();
mbeg_date = begin.getDate()+"/"+calendar_metting_beginmonth+"/"+calendar_metting_beginyear;
beg_time = begin.getHours();

System.out.println("the vaule of mbeg_date="+mbeg_date.toString().trim());
end_date = end.getDate();
end_time = end.getHours();

CallHandlerUI.metting_begin_date.add(beg_date.toString());
CallHandlerUI.metting_begin_mdate.add(mbeg_date.toString());

CallHandlerUI.metting_begin_mtime.add(calendar_metting_begintime.toString());

CallHandlerUI.metting_end_date.add(end_date.toString());
CallHandlerUI.metting_end_time.add(end_time.toString());
CallHandlerUI.metting_end_mtime.add(calendar_metting_endtime.toString());

}
while(eventCursor.moveToNext());
}
}
break;
}
}
}

here is the whole of the code is to be posted which will simply reads each and every events form your calendar with the help of that postback url which is for 2.2 and above version:

Uri.parse("content://com.android.calendar/instances/when").buildUpon();

pl find the under those version lower then 2.2 in android and use those events as you required place...

Getting events from specific calendar Google API Python

You need to select the calendar from which you want to retrieve events passing the correct calendarId to service.events().list(). 'primary' can always be used to get the primary calendar of the user, you can get the information of all the calendars the user has with service.calendarList().list().

How to get today's events from Google Calendar in Android

You can use the method Events: list since you cannot get the current events directly.

You can set the timeMin and timeMax from the start of the current date up to the last hour of the said date.

You can check this SO post for further info.

how to get all events from google calendar using google calendar API

Events: list

Returns events on the specified calendar.

HTTP request

GET https://www.googleapis.com/calendar/v3/calendars/calendarId/events

As you can see from the documentation Events.list returns the events for A calendar. If you have more then one calendar then you will need to request the events for that calendar as well.

Tip: You might want to look at

CalendarList: list Returns entries on the user's calendar list.

This will return a list of the calendars the user has access to then you can loop though the events on each one of them.

How to get all the events from calendar?

Try this code...

public class Utility {

public static ArrayList<String> nameOfEvent = new ArrayList<String>();
public static ArrayList<String> startDates = new ArrayList<String>();
public static ArrayList<String> endDates = new ArrayList<String>();
public static ArrayList<String> descriptions = new ArrayList<String>();

public static ArrayList<String> readCalendarEvent(Context context) {
Cursor cursor = context.getContentResolver()
.query(
Uri.parse("content://com.android.calendar/events"),
new String[] { "calendar_id", "title", "description",
"dtstart", "dtend", "eventLocation" }, null,
null, null);
cursor.moveToFirst();
// fetching calendars name
String CNames[] = new String[cursor.getCount()];

// fetching calendars id
nameOfEvent.clear();
startDates.clear();
endDates.clear();
descriptions.clear();
for (int i = 0; i < CNames.length; i++) {

nameOfEvent.add(cursor.getString(1));
startDates.add(getDate(Long.parseLong(cursor.getString(3))));
endDates.add(getDate(Long.parseLong(cursor.getString(4))));
descriptions.add(cursor.getString(2));
CNames[i] = cursor.getString(1);
cursor.moveToNext();

}
return nameOfEvent;
}

public static String getDate(long milliSeconds) {
SimpleDateFormat formatter = new SimpleDateFormat(
"dd/MM/yyyy hh:mm:ss a");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
}

You can also check this

Getting events from calendar

Get event times from all calendars in googleCal api

Checking that there was a similar question as yours in this thread. I as well checked the documentation for the calendars.events.list and it seems that the API call can only handle one calendarID per call, a solution would be that you make a calendarList.List to retrieve the calendarID of the account you are looking for and then loop with each Id using calendars.events.list.

Edit: I was able to run this code that gets the calendarID for all the calendars that my account has and list the first 5 events:

function listCalendarEvents() {
var calendars;
var pageToken;
do {
calendars = Calendar.CalendarList.list({
maxResults: 100,
pageToken: pageToken
});
if (calendars.items && calendars.items.length > 0) {
for (var i = 0; i < calendars.items.length; i++) {
var calendar = calendars.items[i];
Logger.log('%s (ID: %s)', calendar.summary, calendar.id); // retrieves the calendar information along with the calendar ID
var calendarId = calendar.id; // assigning the calendar ID from the loop.
var now = new Date();
var events = Calendar.Events.list(calendarId, {
timeMin: now.toISOString(),
singleEvents: true,
orderBy: 'startTime',
maxResults: 5
});
if (events.items && events.items.length > 0) { //loop to retrieve the 5 events
for (var j = 0; j < events.items.length; j++) {
var event = events.items[j];
if (event.start.date) {
// All-day event.
var start = new Date(event.start.date);
Logger.log('%s (%s)', event.summary, start.toLocaleDateString());
} else {
var start = new Date(event.start.dateTime);
Logger.log('%s (%s)', event.summary, start.toLocaleString());
}
}
} else {
Logger.log('No events found.');
}
}
} else {
Logger.log('No calendars found.');
}
pageToken = calendars.nextPageToken;
} while (pageToken);
}

Getting Events from Calendar using C# and Microsoft Graph SDK

I don't have an environment to test the c# Graph SDK, but I suspect the underlying query doesn't ask Graph for the calendar events so that's why the field is null.

Using Graph explorer (https://developer.microsoft.com/en-us/graph/graph-explorer#), we can try an equivalent query and note only metadata about the calendars are returned.

GET https://graph.microsoft.com/v1.0/me/calendars

Sample Image

The Graph explorer has some calendar sample queries. They might not be visible by default, so click the 'show more samples' link.

Sample Image

The all events in my calendar sample query is probably what you're looking for. The request is:

GET https://graph.microsoft.com/v1.0/me/events?$select=subject,body,bodyPreview,organizer,attendees,start,end,location

(or /calendars/{calendar-id}/events if you want events for a specific calendar)

The next step is converting this REST API query into the SDK syntax to use in your application.

From this sample, a starting point is:

IUserEventsCollectionPage events = await graphClient.Me.Events.Request().GetAsync();

Besides querying the events, you might also want to look into querying the users calendar view which allows you to specify a start and end date.



Related Topics



Leave a reply



Submit