Tuesday, January 15, 2013

How to check whether a content provider is installed ?

Today, I had to research on the calender to read the events from the native Android calender. Problem is different version of Android uses different content providers. So, how to find if calendar content provider exists ?


private boolean isCalenderExisit(Activity activity) {
boolean isSuccess = false;
ContentProviderClient calendarClient = activity.getContentResolver().acquireContentProviderClient(
getContentURI());
ContentProviderClient eventClient = activity.getContentResolver().acquireContentProviderClient(
getContentURI());
if (eventClient == null || calendarClient == null) {
isSuccess = false;
}
else {
calendarClient.release();
eventClient.release();
isSuccess = true;
}

return isSuccess;
}
public static Uri getContentURI() {
final Uri CONTENT_URI_PRE_8 = Uri.parse("content://calendar/calendars");
final Uri CONTENT_URI = Uri.parse("content://com.android.calendar/calendars");
if(Build.VERSION.SDK_INT <= 7) {
return CONTENT_URI_PRE_8;
} else {
return CONTENT_URI;
}
}

No comments:

Post a Comment