环信集成步骤操作
官网:
1.打开首页
https://www.easemob.com/
2.注册开发者
https://www.jianshu.com/p/1c8e59e62a48
3.下载环信的sdk
https://www.easemob.com/download/im
选择android端下载
4.创建一个项目导入环信sdk,通过4.1和4.2步骤集成
4.1
implementation files('libs/hyphenatechat_3.8.0.jar')
4.2
sourceSets {
main {
jniLibs.srcDirs = ['libs']
jni.srcDirs = []
}
}
5.创建app类.添加启动配置
package com.fenghongzhang.shuaiim;
import android.app.Application;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMOptions;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
EMOptions options = new EMOptions();
// 默认添加好友时,是不需要验证的,改成需要验证
options.setAcceptInvitationAlways(false);
// 是否自动将消息附件上传到环信服务器,默认为True是使用环信服务器上传下载,如果设为 false,需要开发者自己处理附件消息的上传和下载
options.setAutoTransferMessageAttachments(true);
// 是否自动下载附件类消息的缩略图等,默认为 true 这里和上边这个参数相关联
options.setAutoDownloadThumbnail(true);
//初始化
EMClient.getInstance().init(this, options);
//在做打包混淆时,关闭debug模式,避免消耗不必要的资源
EMClient.getInstance().setDebugMode(true);
}
}
6.实现注册功能
public void register(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try {
EMClient.getInstance().createAccount("feng", "123456");//同步方法
} catch (HyphenateException e) {
e.printStackTrace();
}
}
}).start();
}
7.实现登录功能
public void login(View view) {
EMClient.getInstance().login("feng","123456",new EMCallBack() {//回调
@Override
public void onSuccess() {
EMClient.getInstance().groupManager().loadAllGroups();
EMClient.getInstance().chatManager().loadAllConversations();
Log.d("main", "登录聊天服务器成功!");
}
@Override
public void onProgress(int progress, String status) {
}
@Override
public void onError(int code, String message) {
Log.d("main", "登录聊天服务器失败!");
}
});
}
8.发送消息和接收消息
public void send(View view) {
//创建一条文本消息,content为消息文字内容,toChatUsername为对方用户或者群聊的id,后文皆是如此
EMMessage message = EMMessage.createTxtSendMessage("真帅啊", "feng1");
//发送消息
EMClient.getInstance().chatManager().sendMessage(message);
}
public void receiver(View view) {
//第一个是用户名,第二个是会议类型
Map<String, EMConversation> conversations = EMClient.getInstance().chatManager().getAllConversations();
EMConversation feng1 = conversations.get("feng1");
List<EMMessage> allMessages = feng1.getAllMessages();
for (EMMessage allMessage : allMessages) {
EMMessage.Type type = allMessage.getType();
Log.i(TAG, "receiver: "+type);
switch (type){
case TXT:
EMMessageBody body = allMessage.getBody();
Log.i(TAG, "receiver: "+body.toString());
break;
}
}
}