2016-02-26 88 views
1

我正在使用日历提供程序在Google日历中插入事件。问题是我得到的查询结果作为Uri没有任何异常和事件ID也。但我添加的事件没有显示在日历应用程序中。任何人都可以帮助我。我还想为我的活动设置提醒。以下是我用来添加事件的代码。添加的事件没有显示在Android日历应用程序中

 public void addNewEvent() { 
    long startMillis = 0; 
    long endMillis = 0; 
    Calendar beginTime = Calendar.getInstance(); 
    beginTime.set(2016, 4, 1, 7, 30); 
    Log.e("startTime",new SimpleDateFormat("MM:dd:yyyy").format(beginTime.getTimeInMillis())); 
    startMillis = beginTime.getTimeInMillis(); 
    Calendar endTime = Calendar.getInstance(); 
    endTime.set(2016, 4, 1, 8, 56); 
    endMillis = endTime.getTimeInMillis(); 
    // Insert Event 
    Log.e("endTime",new SimpleDateFormat("MM:dd:yyyy").format(endTime.getTimeInMillis())); 
    ContentResolver cr = activity.getContentResolver(); 
    ContentValues values = new ContentValues(); 
    TimeZone timeZone = TimeZone.getDefault(); 
    values.put(CalendarContract.Events.DTSTART, startMillis); 
    values.put(CalendarContract.Events.DTEND, endMillis); 
    values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID()); 
    values.put(CalendarContract.Events.TITLE, "Going for a ride"); 
    values.put(CalendarContract.Events.DESCRIPTION, "Event desc"); 
    values.put(CalendarContract.Events.CALENDAR_ID, 39); 
    values.put(CalendarContract.Events.EVENT_LOCATION,"Malta"); 
    Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values); 
    // Retrieve ID for new event 
    long eventID = Long.parseLong(uri.getLastPathSegment()); 
    setReminder(cr, eventID, 100); 
    Log.e("eventId",eventID+""); 
} 

public void setReminder(ContentResolver cr, long eventID, int timeBefore) { 
     try { 
      ContentValues values = new ContentValues(); 
      values.put(CalendarContract.Reminders.MINUTES, timeBefore); 
      values.put(CalendarContract.Reminders.EVENT_ID, eventID); 
      values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT); 
      Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values); 
      Cursor c = CalendarContract.Reminders.query(cr, eventID, 
        new String[]{CalendarContract.Reminders.MINUTES}); 
      if (c.moveToFirst()) { 
       Log.e("Reminder Uri",uri.toString()); 
       Log.e("","calendar" 
         + c.getInt(c.getColumnIndex(CalendarContract.Reminders.MINUTES))); 
      } 
      c.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

回答

0

上面的代码有助于only.if要事件添加到谷歌日历,那么你必须使用谷歌日历API添加事件,手机日历。将事件添加到电话日历时,我已将日历ID设置为“1”。

+0

电话日历是谷歌日历是不是?因为它与我的所有Google帐户绑定。 – ADM

+0

如果关闭同步,则手机日历中的事件将不会显示在Google日历中。因此,您可以使用Google日历API直接将事件添加到Google日历中 – suneall01

相关问题