在Android 4.0中,日曆與Android 2.3中的Uri相同。所以我附上我的代碼以防其他人遇到同樣的問題。
public void addToCalendar(Context ctx, final String title, final String comment, final long dtstart, final long dtend) {
final ContentResolver cr = ctx.getContentResolver();
Cursor cursor ;
cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id","calendar_displayName" }, null, null, null);
/*if (Integer.parseInt(Build.VERSION.SDK) == 8)
cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
*/
//Get all the calendar ids and name available in the phone
if (cursor.moveToFirst()) {
final String[] calNames = new String[cursor.getCount()];
final int[] calIds = new int[cursor.getCount()];
for (int i = 0; i < calNames.length; i++) {
calIds[i] = cursor.getInt(0);
calNames[i] = cursor.getString(1);
cursor.moveToNext();
}
//Creation of a new event in calendar whose position is 0 on the phone
ContentValues cv = new ContentValues();
cv.put("calendar_id", calIds[0]);
cv.put("title", title);
cv.put("dtstart", dtstart);
cv.put("dtend", dtend);
cv.put("eventTimezone","Spain");
cv.put("description", comment);
//Insertion on the events of the calendar
cr.insert(Uri.parse("content://com.android.calendar/events"), cv);
/*Uri newEvent ;
if (Integer.parseInt(Build.VERSION.SDK) == 8)
newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv);
*/
finish();
}
cursor.close();
}
您有以下的所有信息:http://developer.android.com/guide/topics/providers/calendar-provider.html約翰說過。
固定,你說得對。謝謝;)我要寫我的代碼添加一個事件到一個特定的日曆。事實是,日曆表中的列名已更改。 – ElChencho
很高興能盡我所能幫助你。 –