Reputation: 3022
Is it possble to target Android 2.1 and use CalendarProvider
on devices with Android 4.0 and higher or is it can only be achieved by creating 2 separate APKs?
Upvotes: 0
Views: 241
Reputation: 1007296
That depends on what you mean by "target".
If you mean "set targetSdkVersion" to Android 2.1, you can still use whatever APIs you want, so long as you only try calling them when you are running on a device that has them.
If you mean "set the build target" to Android 2.1, you can still use whatever APIs you want, so long as you use reflection to access the ones that are newer than API Level 7. Since CalendarContract
is a content provider, that mostly is a matter of accessing various static data members, such as CONTENT_URI
. Here is an example of using reflection to get at a CONTENT_URI
value:
private static Uri CONTENT_URI=null;
static {
int sdk=new Integer(Build.VERSION.SDK).intValue();
if (sdk>=5) {
try {
Class<?> clazz=Class.forName("android.provider.ContactsContract$Contacts");
CONTENT_URI=(Uri)clazz.getField("CONTENT_URI").get(clazz);
}
catch (Throwable t) {
Log.e("PickDemo", "Exception when determining CONTENT_URI", t);
}
}
else {
CONTENT_URI=Contacts.People.CONTENT_URI;
}
}
(note: this example is designed to run on Android 1.5 and higher -- depending on your minSdkVersion
, you could use Build.VERSION.SDK_INT
instead of new Integer(Build.VERSION.SDK).intValue()
).
If by "target" you mean something else, then we would need clarification of your use of the verb "target".
Upvotes: 1