刚看完王阳明心学,觉得是时候知行合一了!!!,希望有帮助!!
废话不多说,直接进入主题:
进入融云开发文档:
http://www.rongcloud.cn/docs/android_imlib.html
相信有一定的工作经验的你,关于申请账号和导入jar都不会陌生,跟其他第三方是一样的。但这里有个特殊的地方是获取token。这里的token的概念加是,在你项目申请融云账号的时候,一个token对应一个用户。也就是说tokey是融云平台对用户的唯一标识。
在开发文档也也清楚的教学了如何申请测试token。(注意申请2个,方便你互相发短信)当然在实际开发中,你只要调用后台给你的一个接口获取这个token即可。好了废话不多说,接下来说一下我第一次接遇到的一下疑问点。相信对你们有帮助:
1、根据开发文档,考入jar,和引用model后,AndroidMainfest里的配置。我把配置会话列表和会话界面也贴出来。后面介绍
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lihangrong.leorong">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--会话列表-->
<activity
android:name=".ConversationListActivity"
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="lihangrong.leorong"
android:pathPrefix="/conversationlist"
android:scheme="rong" />
</intent-filter>
</activity>
<!--聚合会话列表-->
<activity
android:name=".SubConversationListActivtiy"
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="lihangrong.leorong"
android:pathPrefix="/subconversationlist"
android:scheme="rong" />
</intent-filter>
</activity>
<!--会话界面-->
<activity
android:name=".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="lihangrong.leorong"
android:pathPrefix="/conversation/"
android:scheme="rong" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="lihangrong.leorong.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/rc_file_path" />
</provider>
</application>
</manifest>
2、在这里要注意,在build里加上官网说的,在defaultConfig里加上。所以说要一步一步按照官网来其实没什么难的
ndk {
abiFilters "armeabi-v7a", "x86"
}
3、配置会话界面直接上代码,当然这里有我但是测试,加上title的测试代码。等你接通了,其他的一些你想要的设置比如聚合和自定义一些,只要严格按照官网不懂的问知识库都能一步一步实现。
public class ConversationActivity extends FragmentActivity {
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conversation);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
String name = getIntent().getData().getQueryParameter("title");
textView.setText(name);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:background="#0000ff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="退出"
android:textColor="#000000"
/>
<TextView
android:id="@+id/textView"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"
/>
</LinearLayout>
<fragment
android:id="@+id/conversation"
android:name="io.rong.imkit.fragment.ConversationFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
4、配置会话列表、
public class ConversationListActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conversationlist);
findViewById(R.id.buttonthis).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(ConversationListActivity.this,"我是李航啊",Toast.LENGTH_SHORT).show();
}
});
}
}
xml文件
<?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">
<Button
android:id="@+id/buttonthis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是这里的Button"
/>
<fragment
android:id="@+id/conversationlist"
android:name="io.rong.imkit.fragment.ConversationListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
5、创建一个APP类,初始化融云。
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
RongIM.init(this);
}
}
5、在MainActivity此时你要知道这里为了测试有2个测试Token,在开发项目中,token会在你用户信息字段里:
为了更好的展示出来,我这里展示用的是我测试时申请的,望众爱卿还得各自去申请,符合王阳明的知行合一啊0 0
记住ID,和token
private String token = "kUKu6xwiVfv/5TSpePdG94kQGZGce6HLsg+kQTqGgzhM9BkvZhEcxW+EW5Qgw5m4hQGzwlYvtXvowHbCbNXG8w==";//李航token 110
private String token2 = "Ut0ZXPV2nJ/ajDTHO8A4QQiLZLUcRci1DJimuPdjO+dakfW6JzE3vWzJQ7Xv8VS4iXLN3xuJVCQ=";//舒小英token 111
(1)、定义一个按钮,点击后连接融云:
RongIM.connect(token, new RongIMClient.ConnectCallback() {
/**
* Token 错误。可以从下面两点检查 1. Token 是否过期,如果过期您需要向 App Server 重新请求一个新的 Token
* 2. token 对应的 appKey 和工程里设置的 appKey 是否一致
*/
@Override
public void onTokenIncorrect() {
}
/**
* 连接融云成功
* @param userid 当前 token 对应的用户 id
*/
@Override
public void onSuccess(String userid) {
Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_SHORT).show();
Log.d("我看看", "--onSuccess" + userid);
}
/**
* 连接融云失败
* @param errorCode 错误码,可到官网 查看错误码对应的注释
*/
@Override
public void onError(RongIMClient.ErrorCode errorCode) {
Log.d("我看看", "--error" + errorCode);
}
});
(2)大功搞成。再定义个button点击启动单聊:
注意这里参数是id,就是说你之前在平台通过id去申请了token,融云平台会根据你的id找到token后。根据token它会自动找到联系的人,这里不用你来担心。
/**
* 启动单聊界面。
*
* @param context 应用上下文。
* @param targetUserId 要与之聊天的用户 Id。
* @param title 聊天的标题,开发者需要在聊天界面通过 intent.getData().getQueryParameter("title")
* 获取该值, 再手动设置为聊天界面的标题。
*/
RongIM.getInstance().startPrivateChat(MainActivity.this, "111", "舒小英");
进阶小知识
其实融云里已经把你想到的没想到的都有进阶学习。这里我总结了几个,对于新手参考和帮助。
1、启动会话列表,会话界面,还是聚合,其实都有个共同方法:
Map<String,Boolean> supportedConversation = new HashMap<>();
supportedConversation.put(Conversation.ConversationType.PRIVATE.getName(), false);
RongIM.getInstance().startConversationList(MainActivity.this,supportedConversation);
相信你应该发现了什么。。
2、如何展示出人的头像呢
(1)、MainActivity实现 RongIM.UserInfoProvider 这个接口,实现其抽象方法,UserInfo提供给融云。当然在实际开发中。通常这里你能获取融云返回给你的id。知道id通过接口能拿到这个人的头像和名字。
别忘记了
RongIM.setUserInfoProvider(this,true);
@Override
public UserInfo getUserInfo(String s) {
Log.d("这下㓟图片没",s);
for (Friend i : list){
if(i.getUserId().equals(s)){
return new UserInfo(i.getUserId(),i.getUserName(), Uri.parse(i.getPortraitUri()));
}
}
return null;
}
(2)我这里也是为了测试,提供了测试的。当然在你申请token的时候用那2个图片。这里的Friend类,自己建。只是存下值,为消息提供者准备
private List<Friend> list = new ArrayList<>();
list.add(new Friend("110","李航","http://www.51zxw.net/bbs/UploadFile/2013-4/201341122335711220.jpg")); list.add(new Friend("111","舒小英","http://img02.tooopen.com/Download/2010/5/22/20100522103223994012.jpg"));
如有不明白的,留言。刚开始玩cdsn,望见谅、 图片收尾