Java 在Android应用程序中获取谷歌日历事件
我刚开始使用Android进行应用程序开发。我想从谷歌日历中提取我的日历事件,然后用它们来计算手机的时间 如:如果活动从下午6点开始,到晚上7点结束: 然后,如果电话时间为下午6:00,但<7:00,则打印“剩余时间”+直到7:00的时间Java 在Android应用程序中获取谷歌日历事件,java,android,events,time,google-calendar-api,Java,Android,Events,Time,Google Calendar Api,我刚开始使用Android进行应用程序开发。我想从谷歌日历中提取我的日历事件,然后用它们来计算手机的时间 如:如果活动从下午6点开始,到晚上7点结束: 然后,如果电话时间为下午6:00,但
有人能给我指一些能让我做到这一点的资源吗?我主要使用Java进行开发,并希望利用一些可用的库来简化此操作。谢谢大家! 我相信这就是你想要的:
Cursor cursor = cr.query(Uri.parse("content://calendar/events"), new String[]{ "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation" }, null, null, null);
//Cursor cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "name" }, null, null, null);
String add = null;
cursor.moveToFirst();
String[] CalNames = new String[cursor.getCount()];
int[] CalIds = new int[cursor.getCount()];
for (int i = 0; i < CalNames.length; i++) {
CalIds[i] = cursor.getInt(0);
CalNames[i] = "Event"+cursor.getInt(0)+": \nTitle: "+ cursor.getString(1)+"\nDescription: "+cursor.getString(2)+"\nStart Date: "+new Date(cursor.getLong(3))+"\nEnd Date : "+new Date(cursor.getLong(4))+"\nLocation : "+cursor.getString(5);
if(add == null)
add = CalNames[i];
else{
add += CalNames[i];
}
((TextView)findViewById(R.id.calendars)).setText(add);
cursor.moveToNext();
}
cursor.close();
Cursor Cursor=cr.query(Uri.parse(“content://calendar/events),新字符串[]{“日历id”、“标题”、“说明”、“dtstart”、“dtend”、“eventLocation”},null,null,null);
//Cursor=cr.query(Uri.parse(“content://calendar/calendars),新字符串[]{“\u id”,“name”},null,null,null);
字符串add=null;
cursor.moveToFirst();
String[]CalNames=新字符串[cursor.getCount()];
int[]CalIds=newint[cursor.getCount()];
对于(int i=0;i
信用证:@Vinay假设您只希望在一个时间间隔内发生事件(否则您很快就会导致太多事件),方法是使用CalendarContract API的助手(请参阅我的博客文章) 以下是了解活动的方法:
long begin = // starting time in milliseconds
long end = // ending time in milliseconds
String[] proj =
new String[]{
Instances._ID,
Instances.BEGIN,
Instances.END,
Instances.EVENT_ID};
Cursor cursor =
Instances.query(getContentResolver(), proj, begin, end);
if (cursor.getCount() > 0) {
// do s.th. meaningful with your result set
}
这个助手的优点是,它负责事件的可见性。例如,删除/取消的事件在下一次同步之前是可见的,否则Android也无法告诉后端删除该事件。你可以自己做,但是如果你想考虑一整天的事件,能见度和不重要,就会变得复杂。始终使用实例 公共静态ArrayList readCalendarEvent(上下文){
public static ArrayList<String> readCalendarEvent(Context context) {
Cursor cursor = context.getContentResolver()
.query(
Uri.parse("content://com.android.calendar/events"),
new String[] { "calendar_id", "title", "description",
"dtstart", "dtend", "eventLocation" }, null,
null, null);
cursor.moveToFirst();
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
// fetching calendars id
nameOfEvent.clear();
startDates.clear();
endDates.clear();
descriptions.clear();
for (int i = 0; i < CNames.length; i++) {
nameOfEvent.add(cursor.getString(1));
startDates.add(getDate(Long.parseLong(cursor.getString(3))));
endDates.add(getDate(Long.parseLong(cursor.getString(4))));
descriptions.add(cursor.getString(2));
CNames[i] = cursor.getString(1);
cursor.moveToNext();
}
return nameOfEvent;
}
Cursor Cursor=context.getContentResolver()
.查询(
解析content://com.android.calendar/events"),
新字符串[]{“日历id”、“标题”、“说明”,
“dtstart”、“dtend”、“eventLocation”},空,
空,空);
cursor.moveToFirst();
//获取日历名称
字符串CNames[]=新字符串[cursor.getCount()];
//获取日历id
name of event.clear();
开始日期。清除();
endDates.clear();
descriptions.clear();
对于(int i=0;i
这看起来是一段不错的代码,但您能为它添加一些上下文吗?如果你能编辑你的帖子并在代码中添加解释,那就太好了。
public static ArrayList<String> readCalendarEvent(Context context) {
Cursor cursor = context.getContentResolver()
.query(
Uri.parse("content://com.android.calendar/events"),
new String[] { "calendar_id", "title", "description",
"dtstart", "dtend", "eventLocation" }, null,
null, null);
cursor.moveToFirst();
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
// fetching calendars id
nameOfEvent.clear();
startDates.clear();
endDates.clear();
descriptions.clear();
for (int i = 0; i < CNames.length; i++) {
nameOfEvent.add(cursor.getString(1));
startDates.add(getDate(Long.parseLong(cursor.getString(3))));
endDates.add(getDate(Long.parseLong(cursor.getString(4))));
descriptions.add(cursor.getString(2));
CNames[i] = cursor.getString(1);
cursor.moveToNext();
}
return nameOfEvent;
}