我正在尝试向 Android 日历 (2.2) 添加条目,但我遇到了一些错误。 我尝试了很多测试但没有成功。请问出了什么问题? 对不起我的英语。
源代码:
String calName;
String calId = null;
String[] projection = new String[] { "_id", "name" };
Uri calendars = Uri.parse("content://com.android.calendar/calendars");
Cursor managedCursor = managedQuery(calendars, projection, "selected=1", null, null);
ContentValues event = new ContentValues();
DateFormat date = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
long time = System.currentTimeMillis();
String timeStr = date.format(time);
Log.d(TAG, "Value of timeStr: " + timeStr);
if (managedCursor.moveToNext()) {
calName = managedCursor.getString(managedCursor.getColumnIndex("name"));
calId = managedCursor.getString(managedCursor.getColumnIndex("_id"));
event.put("calendar_id", calId);
event.put("title", "Event Title");
event.put("description", "Description");
event.put("eventLocation", "New York");
event.put("dtstart", timeStr );
event.put("dtend", timeStr);
Uri eventsUri = Uri.parse("content://com.android.calendar/events");
Uri url = getContentResolver().insert(eventsUri, event);
}
最佳答案
首先,请针对您的错误发布 StackTrace。 第二:在尝试 managedCursor.moveToNext 之前,使用:
managedCursor.moveToFirst();
然后
while(managedCursor.moveToNext()){ };
关于android - 使用 Android 日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5613413/