在 2.0 及以上操作系统中添加日历事件时出现错误
我正在使用下面的代码。它在 android 1.6 上工作正常,但在 android 2.0 及更高版本上抛出以下错误。请让我知道它的解决方案。
错误:
01-24 16:55:28.315:错误/ActivityThread(208):无法找到日历的提供程序信息 01-24 16:55:28.315: ERROR/error(208): 未知 URL content://calendar/events
要阅读活动:
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 (cursor.moveToNext());
}
}
要添加活动
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();
}
}
谢谢
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 (cursor.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该知道,在新的 Android 版本上,日历内容提供程序的 URI 已更改,现在您应该使用 content://com.android.calendar/
是的,这是废话:(
所以 如果您之前使用 content://calendar/ ,为了获得成功,现在您应该使用 content://com.android.calendar/
如果您想保持应用程序的所有 Android 版本之间的兼容性,您将需要处理旧 URI 和新 URI,您可以执行以下操作:
但是,让我们玩一点 xDDD
或者在一行中:
注意:android.os.Build.VERSION.SDK_INT 可用,因为 SDK_INT = 4 我的意思是Android 1.6,对于先前版本 android.os.Build.VERSION.SDK 更多信息位于 http ://developer.android.com/reference/android/os/Build.VERSION
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:
But, lets play a bit xDDD
Or in one line :
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