5

After the launch of android 4, CalendarContract class is introduce to write event in device calendar. But if i have created my app using API level 8 and Now i want to use this CalendarContract class in my project done using API level 8, because in API level 8 i have use undocumented API to write event in device calendar and this code doen't work for android 4. So is it possible to use CalendarContract class in my project without changing the sdk version in manifest file, because if i change the sdk version to 14 then it allow me to use CalendarContract class and by which i can solve the issue for android 4. But on doing so my app not started running in api 8 -13, and which i dont want to do because my app is live on market. So is there any possible way to solve this problem. Please explain me with an example

Rahul
  • 1,667
  • 6
  • 21
  • 38

3 Answers3

3

I managed to produce a work around to this problem today by piecing together different bits of advice found on SO and the wider web.

Basically I use a caretaker called CallendarAccess, and two classes with the same public method structure, one called CalendarLT14 in which the method calls resolve to the old undocumented way, and CalendarGTE14 which utilised the correct Android V14+way of doing things.

Both these classes extend a self written abstract class called CalendarService which contain abstract methods common to the public ones of CalendarLT14 and CalendarGTE14.

A problem in doing this is that Android versions less than IceCreamSandwich (v14) will not compile code such as CalendarContract callendarContact; which is written for later versions. To resolve this one sets the in the manifest the target version to 14 (Android 4.0), but the minimum version to 7 (android 2.1).This means the code 4.0 compiles and presents no problem to early versions because it is never invoked. Example method of the caretaker below.

public CalendarService getInstance()
{
   if (Build.VERSION.SDK_INT >= 14)
           return CalendarGTE14(context)
    else
           return CalendarLT14
}

This solution means the caretaker can selectively return an object which will resolve to the correct code relevant to the Android version in use and completely transparently to the programmer making calls to the caretaker or CalendarService class.

Andrew S
  • 2,847
  • 3
  • 33
  • 50
3

You cannot use CalendarContract on Android devices running less than API Level 14.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thats means i cant solve my problem. why my previous code for writing event in device calendar doesn,t work for android 4. Is it because i have used undocumented Api. Is there any way to solve this out – Rahul Mar 07 '12 at 12:33
  • Abstract the calendar access and have different implementations of the calendar access interface - one for the unpublished & unreliable old Calendar interface and one for CalendarContract? – Jens Mar 07 '12 at 12:52
  • can u please explain me with a simple example. I dont have any idea about that. – Rahul Mar 07 '12 at 13:33
  • This is not a helpful response CommonsWare.With someone of your knowledge I would have expected better, some possible work arounds for example. Why have android not released a compatibility library? – Andrew S May 24 '12 at 09:57
  • 1
    @AndrewS: You assume that there are "some possible work arounds". There are none that are documented and supported. "Why have android not released a compatibility library?" -- Android is an operating system and does not write software. If you really mean "Why has the core Android team not released a compatibility library", you would have to ask them. Somebody else is also welcome to write, document, and support some sort of compatibility library. Nobody has done so to date, AFAIK. – CommonsWare May 24 '12 at 10:18
  • 5
    @AndrewS: Here is a lengthier response to your point, in the form of a blog post: http://commonsware.com/blog/2012/05/24/community-driven-compatibility-library.html – CommonsWare May 24 '12 at 11:27
0

Find a way to find the api level programmatically... if you can then probably you can use appropriate code for diff Api levels... this may help Programmatically obtain the Android API level of a device?

Community
  • 1
  • 1
ngesh
  • 13,398
  • 4
  • 44
  • 60
  • That i know, but after finding the Api level what should i do.can u please explain me – Rahul Mar 07 '12 at 12:42
  • you create a library which has method for getting events.... take api level as argument and based on that use appropriate way to get events.. – ngesh Mar 07 '12 at 12:54
  • can u show me with an example. i dont know how to create my own library and how to add method for getting events – Rahul Mar 07 '12 at 13:35