作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在阅读所有 android 日历事件,使用下面的代码可以正常工作,但是,我想从用户邮件帐户中获取所有谷歌日历事件。我该怎么做?任何建议或示例程序都会非常有帮助!
例如:我想获取xxxxxxxxx@gmail.com(我在android手机上开通的邮件账号)的事件
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("MMMM dd, yyyy (EEEE)");
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/calendars"), (new String[] { "calendar_id", "title", "description", "dtstart", "dtend","eventTimezone", "eventLocation" }), "(" + dtstart + ">" + after + " and " + dtend + "<" + endOfDay.getTimeInMillis() + ")", null, "dtstart ASC");
gCalendar = new ArrayList<GoogleCalendar>();
try {
if (cursor.getCount() > 0) {
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);
dt=convertDate(dtstart1,"hh:mm:ss");
googleCalendar.setDtstart(dt);
String dtend1 = cursor.getString(4);
googleCalendar.setDtend(dtend1);
String eventTimeZone=cursor.getString(5);
googleCalendar.setEventTimeZone(eventTimeZone);
String eventlocation = cursor.getString(6);
googleCalendar.setEventlocation(eventlocation);
}
}
} catch (AssertionError ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
最佳答案
这篇文章有点旧,但这里有另一个在 Android 中获取与 Calendar
content provider 相关的数据的简单解决方案:
使用这个库:https://github.com/EverythingMe/easy-content-providers
现在,获取所有日历:
CalendarProvider calendarProvider = new CalendarProvider(context);
List<Calendar> calendars = calendarProvider.getCalendars().getList();
或者,获取特定日历的所有事件:
List<Event> calendars = calendarProvider.getEvents(calendar.id).getList();
还有获取提醒、参加者、实例的选项。
它适用于列表或光标,并且有一个示例应用程序可以查看它的外观和工作方式。事实上,它支持所有 Android 内容提供程序,例如:联系人、短信、通话......包含所有选项的完整文档:https://github.com/EverythingMe/easy-content-providers/wiki/Android-providers
希望对您有所帮助:)
关于android - 如何在 android 中读取谷歌日历事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20236373/
我是一名优秀的程序员,十分优秀!