how to integrate default app of android in our app programatically - android

Is it possible to integrate the default app(like Calendar) of Android in our app programatically .
Can anyone explain me in details....
The two classes that I am using are:
MainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Example.readCalendar(this);
}
}
and another class Example.java
public class Example {
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
// user has them selected for display.
final Cursor cursor = contentResolver.query(Uri.parse("content://calendar/calendars"),
(new String[] { "_id", "displayName", "selected" }), null, null, null);
// For a full list of available columns see http://tinyurl.com/yfbg76w
HashSet<String> calendarIds = new HashSet<String>();
while (cursor.moveToNext()) {
final String _id = cursor.getString(0);
final String displayName = cursor.getString(1);
final Boolean selected = !cursor.getString(2).equals("0");
System.out.println("Id: " + _id + " Display Name: " + displayName + " Selected: " + selected);
calendarIds.add(_id);
}
// 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://calendar/instances/when").buildUpon();
long now = new Date().getTime();
ContentUris.appendId(builder, now - DateUtils.WEEK_IN_MILLIS);
ContentUris.appendId(builder, now + DateUtils.WEEK_IN_MILLIS);
Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + id,
null, "startDay ASC, startMinute ASC");
// For a full list of available columns see http://tinyurl.com/yfbg76w
while (eventCursor.moveToNext()) {
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);
}
}
}
}
I have added the READ_CALENDAR permission also.

These articles might help you.
Working with the Android Calendar
Accessing the internal calendar database inside Google Android applications
Calendar API (android.provider.Calendar)
Example :
static String contentProvider;
static Uri remindersUri;
static Uri eventsUri;
static Uri calendars;
if(Build.VERSION.RELEASE.contains(”2.2″))
contentProvider = “com.android.calendar”;
else
contentProvider = “calendar”;
remindersUri = Uri.parse(String.format(”content://%s/reminders”,contentProvider));
eventsUri = Uri.parse(String.format(”content://%s/events”,contentProvider));
calendars = Uri.parse(String.format(”content://%s/calendars”,contentProvider));

Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("title", "Some title");
intent.putExtra("description", "Some description");
intent.putExtra("beginTime", eventStartInMillis);
intent.putExtra("endTime", eventEndInMillis);
startActivity(intent);

Related

Read events from android calendar

I have a little problem with my code .I use Android Studio .
I try to make an application to the phone's calendar events displaying time . I don't have any errors but when I tried to run it to my phone
Caused by:
java.lang.NullPointerException
at net.jimblackler.readcalendar.Example.readCalendar(Example.java:29)
at net.jimblackler.readcalendar.MainActivity.onCreate(MainActivity.java:14)
Here is my cod :
Java Class:
import java.util.Date;
import java.util.HashSet;
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 Example {
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
// user has them selected for display.
final Cursor cursor = contentResolver.query(Uri.parse("content://calendar/calendars"),
(new String[] { "_id", "displayName", "selected" }), null, null, null);
HashSet<String> calendarIds = new HashSet<String>();
while (cursor.moveToNext()) {
final String _id = cursor.getString(0);
final String displayName = cursor.getString(1);
final Boolean selected = !cursor.getString(2).equals("0");
System.out.println("Id: " + _id + " Display Name: " + displayName + " Selected: " + selected);
calendarIds.add(_id);
}
// 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://calendar/instances/when").buildUpon();
long now = new Date().getTime();
ContentUris.appendId(builder, now - DateUtils.WEEK_IN_MILLIS);
ContentUris.appendId(builder, now + DateUtils.WEEK_IN_MILLIS);
Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + id,
null, "startDay ASC, startMinute ASC");
while (eventCursor.moveToNext()) {
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);
}
}
}
}
MainActivity:
package net.jimblackler.readcalendar;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Example.readCalendar(this);
}
}
First, please check the uri address . For example 'content://calendar/calendars' , it's not used over Android 2.1.
Before Android 14:
calanderURL = "content://calendar/calendars";
calanderEventURL = "content://calendar/events";
calanderRemiderURL= "content://calendar/reminders";
After:
calanderURL = "content://com.android.calendar/calendars";
calanderEventURL = "content://com.android.calendar/events";
calanderRemiderURL = "content://com.android.calendar/reminders";
However, you'd better use like this:
private Uri calendarsUri = Calendars.CONTENT_URI;
private Uri eventsUri = Events.CONTENT_URI;
private Uri remindersUri = Reminders.CONTENT_URI;
private Uri attendeesUri = Attendees.CONTENT_URI;
Second , please check the table column name . You can print the following columns and have a look .
/** Calendars table columns */
public static final String[] CALENDARS_COLUMNS = new String[] {
Calendars._ID, // 0
Calendars.ACCOUNT_NAME, // 1
Calendars.CALENDAR_DISPLAY_NAME, // 2
Calendars.OWNER_ACCOUNT // 3
};
/** Events table columns */
public static final String[] EVENTS_COLUMNS = new String[] {
Events._ID,
Events.CALENDAR_ID,
Events.TITLE,
Events.DESCRIPTION,
Events.EVENT_LOCATION,
Events.DTSTART,
Events.DTEND,
Events.EVENT_TIMEZONE,
Events.HAS_ALARM,
Events.ALL_DAY,
Events.AVAILABILITY,
Events.ACCESS_LEVEL,
Events.STATUS,
};
/** Reminders table columns */
public static final String[] REMINDERS_COLUMNS = new String[] {
Reminders._ID,
Reminders.EVENT_ID,
Reminders.MINUTES,
Reminders.METHOD,
};
/** Reminders table columns */
public static final String[] ATTENDEES_COLUMNS = new String[] {
Attendees._ID,
Attendees.ATTENDEE_NAME,
Attendees.ATTENDEE_EMAIL,
Attendees.ATTENDEE_STATUS
};
Third, you code manner is very bad .You should declare these above parameters as 'static final' ones .

Getting Attendees details from CalendarContract

I am trying to create a app which will fetch details of Events and Attendees from Calendar app on device.
I am facing the problems which are:
1). In many of the events Title and their attendees does not match.
2). In many of the events I am getting 0 attendees (mainly for upcoming events).
Here is my code: (Please let me know the mistake).
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[] { Calendars._ID, Calendars.NAME}), 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);
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);
Log.e("123", "Calender ID---->>>>>>"+id);
Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { Events.TITLE, "begin", "end", "allDay", Events._ID, Events.CALENDAR_ID}, Events.CALENDAR_ID+"=" + id,
null, "_id ASC");
Log.e("123","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");
final String eventId = eventCursor.getString(4);
final String calendarID = eventCursor.getString(5);
Log.e("123", "Event Id----->>>>>"+eventId+"---------calendarId----->>>"+calendarID);
/* System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
" All Day: " + allDay);
*/
Log.e("123","Title:"+title);
Log.e("123","Begin:"+begin);
Log.e("123","End:"+end);
Log.e("123","All Day:"+allDay);
// Attendees Code
Cursor eventAttendeesCoursor = contentResolver.query(CalendarContract.Attendees.CONTENT_URI, new String []{ Attendees.ATTENDEE_NAME, Attendees.EVENT_ID}, Attendees.EVENT_ID +" = " + eventId, null, null);
Log.e("123", "Count of no of attendees-----"+eventAttendeesCoursor.getCount());
if(eventAttendeesCoursor.getCount()>0)
{
if(eventAttendeesCoursor.moveToFirst())
{
do {
// Log.e("123", "Attendees Name---->>>"+ eventAttendeesCoursor.getString(0));
Log.e("123", "Attendees Event ID---->>>"+ eventAttendeesCoursor.getString(1));
} while(eventAttendeesCoursor.moveToNext());
}
}
}
while(eventCursor.moveToNext());
}
}
break;
}
}
}
I didnt look in details into your code, but i just ran into the same issue - and it was because of a mixup between the instance id and the event id. I did see that your uri (builder) is based on instances, but the field you're requiring is event.id : these are distinct.
The important thing is that the attendees table is based on the EVENT id - and not the instance id - so it's likely to explain your issues.
Put it all in order, consistently (i.e. extracting the EVENT id from the INSTANCE table based on your URI and pass it on to the ATTENDEES table. See if that helps.

Getting attendees details from Calendar Content Provider

I am trying to create a app which will fetch details of Events and Attendeesfrom Calendar app.
I am facing the problems which are"
1). In many of the events Title and their attendees does not match.
2). In many of the events I am getting 0 attendees
(mainly for upcoming events).
Here is my code: (Please let me know the mistake).
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[] { Calendars._ID, Calendars.NAME}), 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);
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);
Log.e("123", "Calender ID---->>>>>>"+id);
Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { Events.TITLE, "begin", "end", "allDay", Events._ID, Events.CALENDAR_ID}, Events.CALENDAR_ID+"=" + id,
null, "_id ASC");
Log.e("123","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");
final String eventId = eventCursor.getString(4);
final String calendarID = eventCursor.getString(5);
Log.e("123", "Event Id----->>>>>"+eventId+"---------calendarId----->>>"+calendarID);
/* System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
" All Day: " + allDay);
*/
Log.e("123","Title:"+title);
Log.e("123","Begin:"+begin);
Log.e("123","End:"+end);
Log.e("123","All Day:"+allDay);
// Attendees Code
Cursor eventAttendeesCoursor = contentResolver.query(CalendarContract.Attendees.CONTENT_URI, new String []{ Attendees.ATTENDEE_NAME, Attendees.EVENT_ID}, Attendees.EVENT_ID +" = " + eventId, null, null);
Log.e("123", "Count of no of attendees-----"+eventAttendeesCoursor.getCount());
if(eventAttendeesCoursor.getCount()>0)
{
if(eventAttendeesCoursor.moveToFirst())
{
do {
// Log.e("123", "Attendees Name---->>>"+ eventAttendeesCoursor.getString(0));
Log.e("123", "Attendees Event ID---->>>"+ eventAttendeesCoursor.getString(1));
} while(eventAttendeesCoursor.moveToNext());
}
}
}
while(eventCursor.moveToNext());
}
}
break;
}
}
}

Get calendar events for ICS android version 4 for a particular date

I m tring to display the calendar events in android version 4 which can give the arraylist for the title and other details for the events like this but unable to display.
I get the error as calendar not found.
public void readCalendar1()
{
ContentResolver contentResolver = this.getContentResolver();
// Fetch a list of all calendars synced with the device, their display names and whether the
// user has them selected for display.
Cursor cursor;
cursor = contentResolver.query(CalendarContract.Calendars.CONTENT_URI, new String[]
{ CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME },
null, null, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + " ASC");
HashSet<String> calendarIds1 = new HashSet<String>();
while (cursor.moveToNext())
{
final String _id = cursor.getString(0);
final String displayName = cursor.getString(1);
System.out.println("Id: " + _id + " Display Name: " + displayName );
calendarIds1.add(_id);
}
// For each calendar, display all the events from the previous week to the end of next week.
for (String id : calendarIds1)
{
Uri builder = CalendarContract.Events.CONTENT_URI;
System.out.println("str in read cal1 "+strconvert+"str2 in read cal1 "+strconvert1);
//strconvert and strconvert1 are string which have the particular dates
Cursor eventCursor = contentResolver.query(builder,
new String[] { CalendarContract.Events.TITLE, strconvert,
strconvert1, CalendarContract.Events.ALL_DAY,CalendarContract.Events.EVENT_LOCATION,
CalendarContract.Events.HAS_ALARM,CalendarContract.Events.DESCRIPTION},
"calendar_id=" + id,null,"dtstart ASC" );
// For a full list of available columns see http://tinyurl.com/yfbg76w
int n=eventCursor.getCount();
System.out.println("No. of rows is="+n);
while(eventCursor.moveToNext())
{
title = eventCursor.getString(0);
begin = new Date(eventCursor.getLong(1));
end = new Date(eventCursor.getLong(2));
allDay = !eventCursor.getString(3).equals("0");
loc=eventCursor.getString(4);
hasalarm = !eventCursor.getString(5).equals("0");
desc=eventCursor.getString(6);
titlestr.add(title);
sdatestr.add(begin.toString());
edatestr.add(end.toString());
locstr.add(loc);
descstr.add(desc);
alarmstr.add(hasalarm.toString());
System.out.println("Title String: " + titlestr);
System.out.println("Begin String: " + sdatestr);
System.out.println("End String: " + edatestr);
System.out.println("Loc String: " + locstr);
System.out.println("Desc String: " + descstr);
System.out.println("Alarm String: " + alarmstr);
// }
System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
" All Day: " + allDay+" Location="+loc+" Descriptn="+desc);
}
}
}
This code is working for me to read for both versions below ics android 4 and above ics
//for os version android bELOW version 4(ICS)
public static boolean eventChecker(Context context,ContentResolver cr,String calID){
Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
long now = new Date().getTime();
ContentUris.appendId(builder, now);
ContentUris.appendId(builder, now + DateUtils.YEAR_IN_MILLIS);
Cursor eventCursorr = cr.query(builder.build(),
new String[] { "title", "begin","description"}, "Calendars._id=" + calID,
null, "startDay ASC, startMinute ASC");
while (eventCursorr.moveToNext()) {
final String titler = eventCursorr.getString(0).trim();
final Date beginr = new Date(eventCursorr.getLong(1));
final String descriptionr = eventCursorr.getString(2).trim();
SimpleDateFormat sdfrr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String stimesr = sdfrr.format(beginr);
System.out.println("titler "+titler +"stimesr "+stimesr +"descriptionr "+descriptionr );
}
}
//for os version android version 4(ICS) AND ABOVE
#TargetApi(14)
public static boolean eventChecker14(Context context,ContentResolver contentResolver,String calID){
Uri builder = CalendarContract.Events.CONTENT_URI;
String[] COLS = new String[]{ CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART,CalendarContract.Events.DESCRIPTION};
Cursor eventCursor = contentResolver.query(builder,
COLS,
"calendar_id=" + calID,null,"dtstart ASC" );
int n=eventCursor.getCount();
System.out.println("No. of rows is="+n);
while(eventCursor.moveToNext())
{
String title1 = eventCursor.getString(0).trim();
Date begin1 = new Date(eventCursor.getLong(1));
String desc1=eventCursor.getString(2).trim();
SimpleDateFormat sdfrr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String stimesr = sdfrr.format(begin1);
System.out.println("title1"+title1+"desc1"+desc1+"stimesr"+stimesr);
}
}

how to get calendar event update on android device

I want to get calendar event updates (when a new event is added or an existing event is deleted ) on android 2.2 devices ?
i am beginner so tell me step by step solution for this..
This code might help you :)
public class myCalendar
{
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
// user has them selected for display.
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)
{
eventCursor.moveToFirst();
while (eventCursor.moveToNext())
{
Object 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);
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();
beg_time = begin.getHours()+":"+begin.getMinutes();
end_date = end.getDate();
end_time = end.getHours()+":"+end.getMinutes();
}
}
break;
}
}
}

Categories

Resources