通过聊天(XMPP)发送日历事件(Android)

3

我正在学习Android,并且想要从手机中向其他人发送日历事件,就像whatsapp发送联系人一样。在聊天(XMPP)中发送联系人时,我使用了vCard。因此,在聊天中发送日历事件,我应该使用什么?

我已经搜索了很多,但是没有找到有用的东西。

请建议我用于在XMPP中发送日历事件的库或代码片段。

提前感谢您。

6个回答

1

清单文件

<uses-permission android:name="android.permission.READ_CALENDAR" />

代码:
Cursor cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id",  "displayname" }, null, null, 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] = cursor.getString(1);
    cursor.moveToNext();
}
cursor.close();

获取所有事件,指定范围可完成特定事件。
ContentResolver contentResolver = getContentResolver();

Uri.Builder builder = Uri.parse(getCalendarUriBase() + "/instances/when").buildUpon();
        long now = new Date().getTime();
        ContentUris.appendId(builder, now - DateUtils.MILLIS_PER_DAY*10000);
        ContentUris.appendId(builder, now + DateUtils.MILLIS_PER_DAY * 10000);

假设您想记录日历ID为1的事件ID

Cursor eventCursor = contentResolver.query(builder.build(),
                new String[] { "event_id"}, "Calendars._id=" + 1,
                null, "startDay ASC, startMinute ASC"); 
        // For a full list of available columns see http://tinyurl.com/yfbg76w
        while (eventCursor.moveToNext()) {
            String uid2 = eventCursor.getString(0);
            Log.v("eventID : ", uid2);

        }

现在在ListView中显示此事件,选择其中一个作为文本消息发送,并在接收方侧:
Calendar cal = Calendar.getInstance();              
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", HERE MSG WHICH YOU RECEIVE);
startActivity(intent);

getCalendarUriBase() 函数定义在哪里? - KRP
内容://日历/日历。 - Digvesh Patel
在安卓系统中没有提供MILLIS_PER_DAY。 - KRP
长时间 = TimeUnit.MILLISECONDS.toDays(毫秒); - Digvesh Patel

1

您可以参考 iCal Import Export,它允许您导入 iCalendar 文件到您的日历中,而无需使用 Google 同步服务。

使用此功能,您可以将所有日历事件导入到 iCalendar 文件中,并将这些事件导出回日历。使用 Calendar Provider,您可以获取所有日历数据,但需要 Android API 级别 14 或更高版本,而使用 iCal Import Export,您可以获取所有日历数据,包括低于 API 级别 的版本。


0

您可以在vCard中添加保存日历信息的新字段,例如:

VCard vCard = new VCard();
vCard.setField("calender","calender info");

日历信息应该是一个字符串,可以是JSON格式的字符串。

接收到日历信息的用户可以加载您的vCard并使用方法。

vCard.load(connection, Jid);
String string=vCard.getField("calender");

0

据我所知,没有相关的库,实际上只需要检索日历事件并将其数据转换为XML即可。不幸的是,在XMPP/XML中表示日历事件的XEP也不存在,因此您必须自己设计表示方法。


0

我可以从哪里下载它? - KRP
你有看到维基百科文章右上角的RFC链接吗? - vitalyster
是的,RFC 5545 是标准。 - KRP

-3

你真的不需要为此目的使用任何特定的库。 你可以查看日历提供程序 他们还有一个很棒的例子!

确保在你的manifest.xml中使用这些权限

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

如果你想发送联系人,请看看这个 Android: 如何从手机导入联系人?

如果它对你有帮助,请点赞!我真的需要它。:)


我不仅想导入日历事件,还想通过短信发送它。就像vCard用于发送联系人一样。 :) - KRP
你可以获取vCard并解析其详细信息,然后使用短信提供商发送文本消息。 - Parth Sane
我想发送日历事件而不是联系人。对于发送联系人,我使用了vCard。对于发送日历事件,我应该使用哪个库? - KRP
请使用此链接:http://developer.android.com/guide/topics/providers/calendar-provider.html。 - Parth Sane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接