可以通過這種方式將插入提醒和事件放在日曆中:
Calendar cal = Calendar.getInstance();
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(New_Quote_Activity.this) + "events");
ContentResolver cr1 = getContentResolver();
// event insert
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", "Text");
values.put("dtstart", cal.getTimeInMillis() + 1*60*1000); // event starts at 11 minutes from now
values.put("dtend", cal.getTimeInMillis()+5760*60*1000); // ends 60 minutes from now
values.put("description", edt_Location.getText().toString());
// values.put("visibility", 0);
values.put("hasAlarm", 1);
values.put("eventTimezone", Time.getCurrentTimezone());
Uri event = cr1.insert(EVENTS_URI, values);
// reminder insert
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(New_Quote_Activity.this) + "reminders");
values = new ContentValues();
values.put("event_id", Long.parseLong(event.getLastPathSegment()));
values.put("method", 1);
values.put("minutes", 2880);
cr1.insert(REMINDERS_URI, values);
對於閱讀活動使用此代碼:
Cursor cursor = cr.query(Uri.parse("content://calendar/events"), new String[]{ "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation" }, null, null, null);
//Cursor cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "name" }, null, null, null);
String add = null;
cursor.moveToFirst();
String[] CalNames = new String[cursor.getCount()];
int[] CalIds = new int[cursor.getCount()];
for (int i = 0; i < CalNames.length; i++) {
CalIds[i] = cursor.getInt(0);
CalNames[i] = "Event"+cursor.getInt(0)+": \nTitle: "+ cursor.getString(1)+"\nDescription: "+cursor.getString(2)+"\nStart Date: "+new Date(cursor.getLong(3))+"\nEnd Date : "+new Date(cursor.getLong(4))+"\nLocation : "+cursor.getString(5);
if(add == null)
add = CalNames[i];
else{
add += CalNames[i];
}
((TextView)findViewById(R.id.calendars)).setText(add);
cursor.moveToNext();
}
cursor.close();
此外,不要忘了添加此權限:
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
試過了,但它給了我其他的日曆事件,需要以某種方式指定的日曆ID –
更新爲獲取calId – Benoit
嗯...很奇怪,你可以插入到數據庫但無法查詢和需要做日曆上的一些邏輯...我將這標記爲答案,即使它不幫助我許多。 –