Android 从日历获取事件
我的问题是,我必须制作一个演示应用程序,我想在其中阅读谷歌日历上的事件,因为我已经手动插入了事件的标题、事件的时间以及整个事件的细节。现在我只需要从日历上读这些事件。Android 从日历获取事件,android,calendar,google-calendar-api,Android,Calendar,Google Calendar Api,我的问题是,我必须制作一个演示应用程序,我想在其中阅读谷歌日历上的事件,因为我已经手动插入了事件的标题、事件的时间以及整个事件的细节。现在我只需要从日历上读这些事件。 为此,我尝试使用gcode(googlecode)API,它提供了日历API类。但我还是看不懂这些事件。好的,我找到了整个概念的答案,即如何使用谷歌日历应用程序与android手机集成 代码:-- 首先,您设置这一行,它将从当前类的另一个类ApplicationSettings.java中读取日历事件 ReadCalendar.R
为此,我尝试使用gcode(googlecode)API,它提供了日历API类。但我还是看不懂这些事件。好的,我找到了整个概念的答案,即如何使用谷歌日历应用程序与android手机集成 代码:-- 首先,您设置这一行,它将从当前类的另一个类ApplicationSettings.java中读取日历事件
ReadCalendar.ReadCalendar(ApplicationSettings.this)代码>
pl在android中找到低于2.2的版本,并根据需要使用这些事件放置…上面的代码非常糟糕(而且似乎在ICS中不起作用-显然列名不同)
此处的页面:
提供了更好的概述。
检索日历的(更)简单代码:
public class CalendarContentResolver {
public static final String[] FIELDS = {
CalendarContract.Calendars.NAME,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.CALENDAR_COLOR,
CalendarContract.Calendars.VISIBLE
};
public static final Uri CALENDAR_URI = Uri.parse("content://com.android.calendar/calendars");
ContentResolver contentResolver;
Set<String> calendars = new HashSet<String>();
public CalendarContentResolver(Context ctx) {
contentResolver = ctx.getContentResolver();
}
public Set<String> getCalendars() {
// Fetch a list of all calendars sync'd with the device and their display names
Cursor cursor = contentResolver.query(CALENDAR_URI, FIELDS, null, null, null);
try {
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String name = cursor.getString(0);
String displayName = cursor.getString(1);
// This is actually a better pattern:
String color = cursor.getString(cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_COLOR));
Boolean selected = !cursor.getString(3).equals("0");
calendars.add(displayName);
}
}
} catch (AssertionError ex) { /*TODO: log exception and bail*/ }
return calendars;
}
}
CalendarProvider calendarProvider = new CalendarProvider(context);
List<Calendar> calendars = calendarProvider.getCalendars().getList();
公共类CalendarContentResolver{
公共静态最终字符串[]字段={
CalendarContract.Calendars.NAME,
CalendarContract.Calendars.CALENDAR\u显示\u名称,
CalendarContract.Calendars.CALENDAR\u颜色,
CalendarContract.Calendars.VISIBLE
};
公共静态最终Uri日历\u Uri=Uri.parse(“content://com.android.calendar/calendars");
ContentResolver ContentResolver;
Set calendars=new HashSet();
公共日历内容解析程序(上下文ctx){
contentResolver=ctx.getContentResolver();
}
公共设置getCalendars(){
//获取与设备同步的所有日历及其显示名称的列表
Cursor Cursor=contentResolver.query(日历URI,字段,null,null);
试一试{
if(cursor.getCount()>0){
while(cursor.moveToNext()){
字符串名称=cursor.getString(0);
String displayName=cursor.getString(1);
//这实际上是一种更好的模式:
String color=cursor.getString(cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_color));
所选布尔值=!cursor.getString(3).equals(“0”);
日历。添加(显示名称);
}
}
}catch(AssertionError ex){/*TODO:log exception and bail*/}
返回日历;
}
}
希望这有帮助 我知道这是一篇古老的帖子,但如果有人在不久的将来需要解决方案,我从阿卡什·塔卡尔的回答中找到了优化解决方案的灵感
问题
具体而言,我在原始代码中发现了一些问题:
检索日历事件的循环过早中断
- 因此,仅检索第一个日历中的事件
使用eventCursor.moveToFirst()跳过每个日历中的第一个事件;然后直接移动到while循环中的下一个事件
未在eventCursor中正确设置日历的id
- “日历._id=“+1,应该是”日历._id=“+id
其他人很难指定自己的时间范围
目前的解决方案不是面向对象的,它有很多优点
可读性和文档不是最好的
解决方案
我在此创建了一个Github库,它返回指定时间范围内的事件对象列表,可在以下位置找到:
可以在“CalendarService/src/dk/CalendarService”下找到源文件
用例
解决方案本身包含两种用于不同目的的不同方法
第一个用例:
CalendarService.readCalendar(class.this)
// where class.this is either your class or the context
此方法将返回+/-1天的事件列表
第二个用例:
CalendarService.readCalendar(class.this)
// where class.this is either your class or the context
您还可以指定自己的时间范围:
CalendarService.readCalendar(class.this, int days, int hours)
例如:
CalendarService.readCalendar(class.this, 2, 5)
这样做将返回+/-2天和+/-5小时内的事件列表
该服务已经过测试,但如果您遇到任何问题,请告诉我。使用此代码获取一天的日历事件
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("23:59:59 " + dateString);
endOfDay.setTime(dateCCC);
cursor = contentResolver.query(Uri.parse("content://com.android.calendar/events"), (new String[] { "calendar_id", "title", "description", "dtstart", "dtend","eventTimezone", "eventLocation" }), "(" + dtstart + ">" + after + " and " + dtend + "<" + endOfDay.getTimeInMillis() + ")", null, "dtstart ASC");
/*String[] COLS={"calendar_id", "title", "description", "dtstart", "dtend","eventTimezone", "eventLocation"};
cursor = contentResolver.query(
CalendarContract.Events.CONTENT_URI, COLS,null, null, null);*/
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();
}
}
}
publicstaticvoidreadcalendarEvent(上下文上下文)引发异常{
ContentResolver ContentResolver=context.getContentResolver();
日历=Calendar.getInstance();
字符串dtstart=“dtstart”;
字符串dtend=“dtend”;
SimpleDataFormat displayFormatter=新的SimpleDataFormat(“MMMM dd,yyyy(EEEE)”;
stime=displayFormatter.format(calendar.getTime());
SimpleDataFormat StartFormat=新SimpleDataFormat(“MM/dd/yy”);
String dateString=startFormatter.format(calendar.getTime());
long after=calendar.getTimeInMillis();
SimpleDataFormat Formatter=新的SimpleDataFormat(“hh:mm:ss mm/dd/yy”);
Calendar endOfDay=Calendar.getInstance();
Date-dateCCC=formatter.parse(“23:59:59”+日期字符串);
endOfDay.setTime(日期CCC);
cursor=contentResolver.query(Uri.parse(“content://com.android.calendar/events(“+dtstart+”>“+after+”和“+dtend+“这篇文章有点老了,但这里有另一个简单的解决方案,可以获取Android中日历
内容提供商的相关数据:
使用此库:
现在,获取所有日历:
public class CalendarContentResolver {
public static final String[] FIELDS = {
CalendarContract.Calendars.NAME,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.CALENDAR_COLOR,
CalendarContract.Calendars.VISIBLE
};
public static final Uri CALENDAR_URI = Uri.parse("content://com.android.calendar/calendars");
ContentResolver contentResolver;
Set<String> calendars = new HashSet<String>();
public CalendarContentResolver(Context ctx) {
contentResolver = ctx.getContentResolver();
}
public Set<String> getCalendars() {
// Fetch a list of all calendars sync'd with the device and their display names
Cursor cursor = contentResolver.query(CALENDAR_URI, FIELDS, null, null, null);
try {
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String name = cursor.getString(0);
String displayName = cursor.getString(1);
// This is actually a better pattern:
String color = cursor.getString(cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_COLOR));
Boolean selected = !cursor.getString(3).equals("0");
calendars.add(displayName);
}
}
} catch (AssertionError ex) { /*TODO: log exception and bail*/ }
return calendars;
}
}
CalendarProvider calendarProvider = new CalendarProvider(context);
List<Calendar> calendars = calendarProvider.getCalendars().getList();
CalendarProvider CalendarProvider=新的CalendarProvider(上下文);
列表日历=calendarProvider.getCalendars().getList();
每个日历都有所有字段,因此您可以获得所需的任何信息:
id、名称、日历C