2011-04-20 31 views
1

我嘗試創建活動並向其添加電子郵件。在Android上使用與會者電子郵件創建活動

我不能得到它的工作,我得到的時間是1970年,沒有電子郵件apears。

有什麼想法?

謝謝。

這是我使用的代碼:

Uri calendars = null; 
    if (Integer.parseInt(Build.VERSION.SDK) == 8) 
    { 
     calendars = Uri.parse("content://com.android.calendar/calendars"); 
    } 
    else 
    { 
      calendars = Uri.parse("content://calendar/calendars"); 
    } 

    Cursor cursor = context.getContentResolver().query(calendars,                 new String[] { "_id", "displayName" },             "selected=1",             null,             null); 

    if (cursor != null && cursor.moveToFirst()) 
    { 
     String[] calNames = new String[cursor.getCount()]; 
    final int[] calIds = new int[cursor.getCount()]; 
    for (int i = 0; i < calNames.length; i++) 
    { 
    // retrieve the calendar names and ids    
    calIds[i] = cursor.getInt(0); 
    calNames[i] = cursor.getString(1); 
    cursor.moveToNext(); 
    } 
cursor.close(); 
    String name = "Name1"; 
    String eMail = "[email protected]"; 
    ContentValues event = new ContentValues(); 

    int cal_id = calIds[which]; 
    event.put("calendar_id", cal_id); 
    event.put("title", "test title"); 
    event.put("eventLocation", "test location");    
    event.put("eventStatus", 1); 
    event.put("visibility", 0); 
    event.put("transparency", 0); 
    event.put("hasAlarm", 1); 
    event.put("hasAttendeeData", "1"); 

    Calendar c = Calendar.getInstance(); 
    c.roll(Calendar.DAY_OF_MONTH, true); 

    Date date = c.getTime(); 
    Date begine = new Date(date.getYear(), 
      date.getMonth(), 
      date.getDate(), 
      14, 0);   long dtstart = begine.getTime(); 
    event.put("dtstart", dtstart); 

    Date end = new Date(date.getYear(), 
       date.getMonth(), 
       date.getDate(), 
       15, 0); 
    long dtend = end.getTime(); 

    event.put("dtend", dtend); 
Uri eventsUri = null; 
    if (Integer.parseInt(Build.VERSION.SDK) == 8) 
    { 
     eventsUri = Uri.parse("content://com.android.calendar/events"); 
    } 
    else 
{ 
     eventsUri = Uri.parse("content://calendar/events"); 
    } 
    Uri url = context.getContentResolver().insert(eventsUri, event); 
    long id = -1; 

    if (url != null) 
    { 
     id = Long.parseLong(url.getLastPathSegment()); 
     ContentValues values = new ContentValues(); 
     values.put("event_id", id); 
     values.put("method", 1); //METHOD_ALERT 
     values.put("minutes", 15); // 15 minutes 
     Uri reminder = Uri.parse("content://com.android.calendar/reminders"); 
      context.getContentResolver().insert(reminder, values); 
     if(name.length() > 0 || eMail.length() > 0) 
     { 
      ContentValues attendees = new ContentValues(); 
      attendees.put("attendeeEmail", eMail); 
      attendees.put("attendeeName", name); 
      attendees.put("attendeeRelationship", 2);//RELATIONSHIP_ATTENDEE 
      attendees.put("attendeeStatus", 3); //ATTENDEE_STATUS_INVITED  
      attendees.put("attendeeType", 1); //TYPE_REQUIRED 
      attendees.put("event_id", id); 

      Uri attendeesUri = null; 
      if (Integer.parseInt(Build.VERSION.SDK) == 8) 
      { 
        attendeesUri = Uri.parse("content://com.android.calendar/attendees"); 
      } 
       else 
       { 
        attendeesUri = Uri.parse("content://calendar/attendees"); 
       } 
      context.getContentResolver().insert(attendeesUri, attendees); 

      Intent i = new Intent(Intent.ACTION_EDIT); 

      i.setType("vnd.android.cursor.item/event"); 
      i.setData(url); 

      context.startActivity(i); 
     } 
     else 
     { 
      Toast.makeText(context, "Could not create an event!", Toast.LENGTH_LONG);    
     } 

回答

0

的時間DTSTART和DTEND應在時代之後毫秒(1970年1月1日)使用這個鏈接epoch time converter來獲取當前時間。將它在該網站上的時間乘以1000,因爲它以秒爲單位而不是毫秒返回它。

此外,dtEnd和dtStart是「長」類型。

Long dtStart 
Long dtEnd 
1

校正是這裏....

而不是attendees.put("attendeeRelationship", 2);//RELATIONSHIP_ATTENDEE

你必須把attendees.put("attendeeRelationship", 1);//RELATIONSHIP_ATTENDEE。只需進行更改,即可隨時獲得電子郵件地址和與會者的姓名。

這裏2organizer

1attendees

而且look here

相關問題