更新时间: 2021-03-08
# 功能描述
融云 IMKit 已经实现了一个默认的会话视图控制器,直接使用或继承此类,即可快速启动和使用会话界面。
# 效果展示

# 构建会话
IMkit 中提供了 ConversationFragment 类, 构建后即可使用会话页面的功能.
构建步骤
创建会话
ConversationActivity
以及 Activity 所对应的 xml 布局activity_conversation.xml
.activity_conversation.xml
布局中创建 View 容器, 用于动态添加 ConversationFragment.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
已复制
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- 在
ConversationActivity
的onCreate
方法中创建并使用 ConversationFragment.
class ConversationActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_conversation); // 添加会话界面 ConversationFragment conversationFragment = new ConversationFragment(); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.container, conversationFragment); transaction.commit(); } }
已复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
AndroidManifest.xml
配置ConversationActivity
SDK 内部通过隐式调用的方式来实现跳转到会话列表界面。需在 AndroidManifest.xml
中配置会话列表 ConversationActivity 下面配置 intent-filter
.
- Activity 必须配置
intent-filter
, 否则将无法正常启动会话页面 android:host
的值需填写 App 的 ApplicationId
<activity android:name="xxx.xxx.ConversationActivity" android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden|adjustResize"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:host="${applicationId}" android:pathPrefix="/conversation/" android:scheme="rong" /> </intent-filter> </activity>
已复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 启动会话界面
SDK 内部通过隐式调用的方式来跳转到会话列表界面.
# 普通启动
参数说明
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
context | Context | 是 | 应用上下文 |
conversationType | ConversationType (opens new window) | 是 | 会话类型. 当前为 ConversationType.PRIVATE |
targetId | String | 是 | 接收方 ID |
title | String | 是 | 会话标题. 开发者需要在会话界面通过 intent.getData().getQueryParameter("title") 获取 |
bundle | Bundle | 否 | 扩展参数 |
代码示例
ConversationType conversationType = ConversationType.PRIVATE; String targetId = "接收方 ID"; String title = "这里可以填写名称"; RongIM.getInstance().startConversation(context , conversationType, targetId, title, null)
已复制
1
2
3
4
5
2
3
4
5
# 指定消息位置启动
启动会话界面,并跳转到指定的消息位置
参数说明
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
context | Context | 是 | 应用上下文 |
conversationType | ConversationType (opens new window) | 是 | 会话类型.当前为 ConversationType.PRIVATE |
targetId | String | 是 | 接收方 ID |
title | String | 是 | 会话标题。开发者需要在会话界面通过 intent.getData().getQueryParameter("title")获取 |
fixedMsgSentTime | long | 是 | 用于定位的消息的 sentTime |
代码示例
ConversationType conversationType = ConversationType.PRIVATE; String targetId = "接收方 ID"; String title = "这里可以填写名称"; String fixedMsgSentTime = message.getSentTime();// 要定位的消息的时间 RongIM.getInstance().startConversation(context , conversationType, targetId, title, fixedMsgSentTime)
已复制
1
2
3
4
5
6
2
3
4
5
6