gpt4 book ai didi

Android 在平板电脑中获取日历事件

转载 作者:太空狗 更新时间:2023-10-29 15:20:23 24 4
gpt4 key购买 nike

我正在使用以下代码在我的应用程序中获取日历事件。它在 Android 手机上运行良好,但当我在 Android 平板电脑上尝试此代码时,我的应用程序崩溃了。所以我不知道到底是什么问题以及为什么它在平板电脑上不起作用。

public void syncCalander() {
try {

nameValues = new ArrayList<NameValuePair>();
StringBuffer calbuffers;
int cnt = 1;
StringBuffer calbufferimeis = new StringBuffer();

ContentResolver contentResolver = getApplicationContext()
.getContentResolver();
final Cursor cursor = contentResolver.query(
Uri.parse("content://com.android.calendar/calendars"),
(new String[] { "_id", "displayName", "selected" }), null,
null, null);
if (cursor.getCount() == 0) {

} else {

HashSet<String> calendarIds = new HashSet<String>();
CalendarModel calModel = new CalendarModel();

CalendarModel.CALENDERLIST.add(calModel);
int val = cursor.getCount();
Log.i("=============total event============>", "." + val);
while (cursor.moveToNext()) {

final String _id = cursor.getString(0);
final String displayName = cursor.getString(1);
final Boolean selected = !cursor.getString(2).equals("0");
calModel.setCalendarEvent(displayName);
CalendarModel.CALENDERLIST.add(calModel);
Log.i("--------Display Name----------", "" + "Id: " + _id
+ " Display Name: " + displayName + " Selected: "
+ selected);
calendarIds.add(_id);
Log.i("============celenderIDs==========>", "."
+ calendarIds);
}

for (String id : calendarIds) {

Uri.Builder builder = Uri.parse(
"content://com.android.calendar/instances/when")
.buildUpon();
long now = new Date().getTime();
ContentUris.appendId(builder, now
- DateUtils.WEEK_IN_MILLIS);
ContentUris.appendId(builder, now
+ DateUtils.WEEK_IN_MILLIS);

Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { "title", "begin", "end", "allDay" },
"Calendars._id=" + id, null,
"startDay ASC, startMinute ASC");
Log.i("============cursor size===========>", "."
+ eventCursor.getCount());

while (eventCursor.moveToNext()) {
final String title = eventCursor.getString(0);
final Date begin = new Date(eventCursor.getLong(1));
final Date end = new Date(eventCursor.getLong(2));
final Boolean allDay = !eventCursor.getString(3)
.equals("0");
calModel.setCalendarDate(begin.toString());
CalendarModel.CALENDERLIST.add(calModel);
Log.i("-----Title--------", "Title: " + title
+ " Begin: " + begin + " End: " + end
+ " All Day: " + allDay);
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String date = formatter.format(begin);

int callength = CalendarModel.CALENDERLIST.size();

calbuffers = new StringBuffer();
calbuffers.append("{\"Calenderevent\":\"" + title
+ "\"," + "\"Calenderdate\":\"" + date + "\"}");

calbuffers.append(",");
calbufferimeis.append(calbuffers);

}
eventCursor.close();
}

}
cursor.close();
} catch (Exception e) {
}

}

最佳答案

我用过

content://calendar/calendars

代替

content://com.android.calendar/calendars

针对安卓平板应用

关于Android 在平板电脑中获取日历事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8373962/

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