Reputation: 1667
I use the undocumented API's for adding calendar event for Android OS 2.2. Now Android OS 4 is out, naturally the API's I used don't work! I looked at the Android SDK regarding the calendar api's in level 14 but I'd not able to found a clue how to use this in my project that I have created using Android OS 2.2. Because when I use CalendarContract
class in my project it shows error. So I am not able to find the clue what to do and how to used this class in my project that I have created using Android OS 2.2.
If anyone has any samples to share or a link to samples, please let me know.
Calendar Api Class
public abstract class CalendarAPI
{
private static CalendarAPI api;
public static CalendarAPI getAPI()
{
String apiClass;
if (Integer.parseInt(Build.VERSION.SDK) > Build.VERSION_CODES.FROYO) {
apiClass = "com.example.calendar.CycleManagerSDK8 ";
} else {
apiClass = "com.example.calendar.CycleManagerSDK14 ";
}
try
{
Class<? extends CalendarAPI> realClass = Class.forName(apiClass).asSubclass(CalendarAPI.class);
api = realClass.newInstance();
}
catch (Exception e)
{
throw new IllegalStateException(e);
}
return api;
}
public abstract boolean setAlertOnDevice(Context c) ;
}
class for SDKVersion 8 - 13
public class CycleManagerSDK8 extends CalendarAPI
{
public boolean setAlertOnDevice(Context c)
{
Resources res = c.getResources();
Uri EVENTS_URI = Uri.parse("content://com.android.calendar/" + "events");
Uri REMINDERS_URI = Uri.parse("content://com.android.calendar/" + "reminders");
ContentResolver cr = c.getContentResolver();
Date dtStartDate = getStartDate();
Calendar cal = Calendar.getInstance();
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, m_iStart);
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.set(Calendar.MINUTE, DEFAULT_TIME_OF_DATE);
cal.set(Calendar.SECOND, DEFAULT_TIME_OF_DATE);
cal.set(Calendar.MILLISECOND, DEFAULT_TIME_OF_DATE);
String str = m_reminderText + res.getString(R.string.alert_start);
m_strDescription = res.getString(R.string.alert_start_msg);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", m_strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
m_calendarEvents[m_calendarEventCount] = event;
m_calendarEventCount = m_calendarEventCount + 1;
values = new ContentValues();
values.put("event_id", Long.parseLong(event.getLastPathSegment()));
values.put("method", 1);
values.put("minutes", 10);
cr.insert(REMINDERS_URI, values);
}
}
class for SDKVersion 14
public class CycleManagerSDK14 extends CalendarAPI
{
public void setAlertOnDevice(Context c)
{
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 2, 2, 7, 0);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 2, 2, 7, 0);
endMillis = endTime.getTimeInMillis();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, "Walk The Dog");
values.put(CalendarContract.Events.DESCRIPTION, "My dog is bored, so we're going on a really long walk!");
values.put(CalendarContract.Events.CALENDAR_ID, 1);
values.put(CalendarContract.Events.EVENT_TIMEZONE, "eventTimezone");
cr.insert(CalendarContract.Events.CONTENT_URI, values);
}
}
Thank you.
Upvotes: 5
Views: 2666
Reputation: 2415
As I understand, you have 2 problems...
My suggestion would be Create a abstract class and define the individual implementation.
For example
String apiClass;
if (Integer.parseInt(Build.VERSION.SDK) > Build.VERSION_CODES.DONUT) {
apiClass = "com.example.calendar.version8plus";
} else {
apiClass = "com.example.calendar.version14plus";
}
try {
Class<? extends CalendarAPI> realClass = Class.forName(apiClass).asSubclass(CalendarAPI.class);
api = realClass.newInstance();
} catch (Exception e) {
throw new IllegalStateException(e);
}
With this, you create two different implementation as per the apis...
While compiling depending on your API level exclude the other file from compiling, for this you need to change the API level in Manifest.
Upvotes: 3