gpt4 book ai didi

android - 如何从日历中获取所有事件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:41:16 24 4
gpt4 key购买 nike

您好,我可以使用此代码获取事件

cur = null;
cr = getContentResolver();


// Construct the query with the desired date range.
Uri.Builder builder = Instances.CONTENT_URI.buildUpon();

ContentUris.appendId(builder, 0);
ContentUris.appendId(builder, endMillis);


// Submit the query
cur = cr.query(builder.build(),
INSTANCE_PROJECTION,
null,
null,
null);

但是如您所见,我只能获得给定持续时间内的事件,但我需要所有事件(没有任何时间规范,不包括重复事件)这可能吗?如何 ?请帮助我。

cur =  cr.query(Uri.parse("content://com.android.calendar/events"), 
INSTANCE_PROJECTION,
null,
null,
null);

它给出错误 java.lang.IllegalArgumentException:

最佳答案

试试这个代码...

public class Utility {

public static ArrayList<String> nameOfEvent = new ArrayList<String>();
public static ArrayList<String> startDates = new ArrayList<String>();
public static ArrayList<String> endDates = new ArrayList<String>();
public static ArrayList<String> descriptions = new ArrayList<String>();

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;
}

public static String getDate(long milliSeconds) {
SimpleDateFormat formatter = new SimpleDateFormat(
"dd/MM/yyyy hh:mm:ss a");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
}

你也可以看看这个

Getting events from calendar

关于android - 如何从日历中获取所有事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13232717/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com