2016-05-13 118 views
0

我使用此代码,并从日历中获取事件,它工作正常,但我想从谷歌帐户日历中获取事件。例如,我想获取(deepakcando90 @ gmail。 com)谷歌日历帐户事件也?我知道这是可能的,但不知道如何实现它?谷歌日历获取事件

 public static void readCalendarEvent(Context context) throws ParseException { 
    ContentResolver contentResolver = context.getContentResolver(); 
    Calendar calendar = Calendar.getInstance(); 
    String dtstart = "dtstart"; 
    String dtend = "dtend"; 


    SimpleDateFormat displayFormatter = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); 

    stime=displayFormatter.format(calendar.getTime());  

    SimpleDateFormat startFormatter = new SimpleDateFormat("MM/dd/yy"); 
    String dateString = startFormatter.format(calendar.getTime()); 

    long after = calendar.getTimeInMillis(); 
    SimpleDateFormat formatterr = new SimpleDateFormat("hh:mm:ss MM/dd/yy"); 
    Calendar endOfDay = Calendar.getInstance(); 
    Date dateCCC = formatterr.parse("47:59:59 " + dateString); 
    endOfDay.setTime(dateCCC); 



    cursor = contentResolver.query(Uri.parse("content://com.android.calendar/events"), (new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation" }), "(" + dtstart + ">" + after + " and " + dtend + "<" + endOfDay.getTimeInMillis() + ")", null, "dtstart ASC"); 
    gCalendar = new ArrayList<GoogleCalendar>(); 
    try { 
     System.out.println("Count=" + cursor.getCount()); 

     if (cursor.getCount() > 0) { 

      System.out.println("the control is just inside of the cursor.count loop"); 
      while (cursor.moveToNext()) { 
       GoogleCalendar googleCalendar = new GoogleCalendar(); 
       gCalendar.add(googleCalendar); 
       int calendar_id = cursor.getInt(0); 
       googleCalendar.setCalendar_id(calendar_id); 
       String title = cursor.getString(1); 
       googleCalendar.setTitle(title); 
       String description = cursor.getString(2); 
       googleCalendar.setDescription(description); 
       String dtstart1 = cursor.getString(3); 
       googleCalendar.setDtstart(dtstart1); 
       String dtend1 = cursor.getString(4); 
       googleCalendar.setDtend(dtend1); 
       String eventlocation = cursor.getString(5); 
       googleCalendar.setEventlocation(eventlocation); 

      } 
     } 
    } catch (AssertionError ex) { 
     ex.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

}

回答

0

你可以尝试OAuth 2.0 service accounts正常访问的用户帐户。使用服务帐户可让您模拟用户并代表他们执行操作。

访问域的日历作为一个应用程序

应用程序可以访问域拥有的日历,而不需要用户凭证,如果它验证使用service account。服务帐户必须使用domain-wide authority delegation进行必要的访问。为了模拟用户帐户,请使用GoogleCredential工厂的setServiceAccountUser方法指定用户帐户的电子邮件地址。

我希望它有帮助。 Goodluck :)