2016-05-13 117 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 :)