1:注册账号
2:下载sdk
3:导入modle EaseUI
4:讲model加入到主项目
5:登陆功能:代码如下:
EMClient.getInstance().login(userName,password,new EMCallBack() {//回调
@Override
public void onSuccess() {
EMClient.getInstance().groupManager().loadAllGroups();
EMClient.getInstance().chatManager().loadAllConversations();
Log.d("main", "登录聊天服务器成功!");
COnstact();
}
@Override
public void onProgress(int progress, String status) {
}
@Override
public void onError(int code, String message) {
Log.d("main", "登录聊天服务器失败!");
}
});
6:当登陆成功以后,跳转到联系人列表:
联系人代码如下:
List<String>usernames=
EMClient.getInstance().contactManager().getAllContactsFromServer();
备注:获取联系人必须在子线程执行,更新UI必须在主线程执行.
7:跳转到聊天页面,代码如下:
//new出EaseChatFragment或其子类的实例
EaseChatFragment chatFragment = new EaseChatFragment();
//传入参数
Bundle args = new Bundle();
args.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE);
args.putString(EaseConstant.EXTRA_USER_ID, username);
chatFragment.setArguments(args);
getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
8:添加好友功能,代码如下:
EMClient.getInstance().contactManager().addContact("13934127774", "你好");
注意:第一个参数是要加好友的用户名,第二个参数是添加的理由.
添加好友必须在子线程执行.
9:同意加好友功能,代码如下:
EMClient.getInstance().contactManager().acceptInvitation(username);
注意:参数是好友用户名.同意添加好友功能必须在子线程执行.
10:拒绝好友功能,代码如下:
EMClient.getInstance().contactManager().declineInvitation(username);
注意:参数为好友用户名,拒绝好友功能必须在子线程执行.
11:监听好友动态功能,代码如下:
EMClient.getInstance().contactManager().setContactListener(new EMContactListener() {
@Override
public void onContactAgreed(String username) {
//好友请求被同意
Log.d("username", "好友请求被同意" + username);
}
@Override
public void onContactRefused(String username) {
//好友请求被拒绝
Log.d("username", "好友请求被拒绝" + username);
}
@Override
public void onContactInvited(String username, String reason) {
//收到好友邀请
Log.d("username", "收到好友邀请" + username);
Message msg = mhandler.obtainMessage();
msg.what = 2;
msg.obj = username;
handler.sendMessage(msg);
}
@Override
public void onContactDeleted(String username) {
//被删除时回调此方法
Log.d("username", "被删除时回调此方法" + username);
}
@Override
public void onContactAdded(String username) {
//增加了联系人时回调此方法
Log.d("username", "增加了联系人时回调此方法" + username);
}
});
12:即时得到聊天消息功能,代码如下,一般下载application里面
//聊天消息
public void ChatListener(){
EMMessageListener msgListener = new EMMessageListener() {
@Override
public void onMessageReceived(List<EMMessage> messages) {
//收到消息
Log.d("gggggg",messages.toString());
}
@Override
public void onCmdMessageReceived(List<EMMessage> messages) {
//收到透传消息
}
@Override
public void onMessageReadAckReceived(List<EMMessage> messages) {
//收到已读回执
}
@Override
public void onMessageDeliveryAckReceived(List<EMMessage> message) {
//收到已送达回执
}
@Override
public void onMessageChanged(EMMessage message, Object change) {
//消息状态变动
}
};
EMClient.getInstance().chatManager().addMessageListener(msgListener);
}
注意:在导入环信的时候可能遇到V4包与V7包 的冲突,此时需要移除v4包.
解决方案如下:
在build.grade里面v7包下加入加入 exclude module: 'support-v4'如下代码:
compile ('com.android.support:appcompat-v7:23.4.0'){
exclude module: 'support-v4'
}