Getting error in 2.0 and above OS when adding calendar event
I am using below code. Its working fine for android 1.6 but its throwing below error for android 2.0 and above version. Please let me know the solution for it.
Error:
01-24 16:55:28.315: ERROR/ActivityThread(208): Failed to find provider info for calendar 01-24 16:55:28.315: ERROR/error(208): Unknown URL content://calendar/events
To read Event:
private void readContent(String uriString) {
Uri uri = Uri.parse(uriString);
Cursor cursor = getContentResolver().query(uri, null, null,
null, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
String columnNames[] = cursor.getColumnNames();
String value = "";
String colNamesString = "";
do {
value = "";
for (String colName : columnNames) {
value += colName + " = ";
value += cursor.getString(cursor.getColumnIndex(colName))
+ " ||";
}
Log.e("INFO : ", value);
} while (curso开发者_运维百科r.moveToNext());
}
}
To Add event
private void addEvent(){
try {
ContentValues event = new ContentValues();
event.put("calendar_id", "1");
event.put("title", "tet event");
event.put("description", "hello this is testing of event");
event.put("eventLocation", "Ahmedabad");
Calendar c = Calendar.getInstance();
long date = c.getTimeInMillis();
event.put("dtstart", date);
event.put("dtend", date);
event.put("allDay", 1);
event.put("eventStatus", 1);
event.put("hasAlarm", 1);
Uri eventsUri = Uri.parse("content://calendar/events");
Uri url = getContentResolver().insert(eventsUri, event);
Log.e("uri", url.toString());
} catch (Exception e) {
Log.e("error", e.getMessage());
e.printStackTrace();
}
}
Thanks
You should know, that on new Android versions the URI for Calendar content provider has changed, now you should use content://com.android.calendar/
Yes it´s a crap :(
So if you was using content://calendar/ ,to get successful,now you should use content://com.android.calendar/
If you want to maintain a compatibility across all Android versions of your apps, you will need to handle the Old URI along with the New URI, you can do something like this:
Uri calendarUri;
Uri eventUri;
if (android.os.Build.VERSION.SDK_INT <= 7 )
{
//the old way
calendarUri = Uri.parse("content://calendar/calendars");
eventUri = Uri.parse("content://calendar/events");
}
else
{
//the new way
calendarUri = Uri.parse("content://com.android.calendar/calendars");
eventUri = Uri.parse("content://com.android.calendar/events");
}
But, lets play a bit xDDD
function Uri getCalendarURI(eventUri boolean){
Uri calendarURI = null;
if (android.os.Build.VERSION.SDK_INT <= 7 )
{
calendarURI = (eventUri)?Uri.parse("content://calendar/events"):Uri.parse("content://calendar/calendars");
}
else
{
calendarURI = (eventUri)?Uri.parse("content://com.android.calendar/events"): Uri.parse("content://com.android.calendar/calendars");
}
return calendarURI;
}
Or in one line :
function Uri getCalendarUri(eventUri boolean){
return (android.os.Build.VERSION.SDK_INT <= 7 )?((eventUri)?Uri.parse("content://calendar/events"):Uri.parse("content://calendar/calendars")):(calendarURI = (eventUri)?Uri.parse("content://com.android.calendar/events"): Uri.parse("content://com.android.calendar/calendars"));
}
Note : android.os.Build.VERSION.SDK_INT is available since SDK_INT = 4 i mean Android 1.6,for prior version android.os.Build.VERSION.SDK more info at http://developer.android.com/reference/android/os/Build.VERSION
精彩评论