2014-02-13 101 views
0

我用下面的代碼中插入一個事件(以編程方式,而不是意圖)到特定的日曆(與它的ID)閱讀和插入事件到日曆

 ContentResolver cr = context.getContentResolver(); 
     ContentValues values = new ContentValues(); 
     values.put(CalendarContract.Events.DTSTART, startMillis); 
     values.put(CalendarContract.Events.DTEND, endMillis); 
     values.put(CalendarContract.Events.TITLE, cname); 
     values.put(CalendarContract.Events.DESCRIPTION, note); 
     values.put(CalendarContract.Events.CALENDAR_ID, calID); 
     values.put(CalendarContract.Events.EVENT_TIMEZONE, tz.getDisplayName()); 
     if (recurrence != null) 
      values.put(CalendarContract.Events.RRULE, "FREQ=" + recurrence + ";"); 
     Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values); 

插入的偉大工程,我節省事件ID: uri.getLastPathSegment()

但問題是如何從特定日曆閱讀本特定事件

感謝您的幫助

回答

0

您可以使用ContentResolver的一個選擇查詢的EVENTID

CalendarContract.Events要獲取calendarId這件事情是這樣的:

Uri calUri = CalendarContract.Calendars.CONTENT_URI.buildUpon() 
      .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, mAccountName) 
      .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, mAccountType) 
      .build(); 

getContentResolver().query(calUri, new String[]{CalendarContract.Calendars._ID}, null, null, null); 
+0

試過了,但它給了我其他的日曆事件,需要以某種方式指定的日曆ID –

+0

更新爲獲取calId – Benoit

+0

嗯...很奇怪,你可以插入到數據庫但無法查詢和需要做日曆上的一些邏輯...我將這標記爲答案,即使它不幫助我許多。 –

0

可以通過這種方式將插入提醒和事件放在日曆中:

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"/> 
+0

他問如何檢索它,而不是如何插入它。另外,您應該使用常量代替硬編碼字符串 – Benoit

+0

,您可以通過將此代碼放入功能並激情使用您想要插入事件的值。 –

+0

他想要在日曆中閱讀和插入活動。 –