最近在项目中用到了即使用讯,客户要求用环信,我擦,第一次做,坑啊,网上对这个没有特别明确的使用教程,环信的官网也不像其他的第三方有明确的使用方法,只是说了一个简单的集成,看其他人的博客感觉都说的很麻烦,很含糊,所以现在项目完成了,做个简单的总结,希望能帮助到还不太会的童鞋~~
第一步,集成步骤
下载官方发demo,把easeui以Moudle的形式集成到自己的项目中
到这里就算集成完事了,接下来实现功能
1,首先实现基础的聊天功能
自己写一个Activity,比如我自己写了一个ChatRoomActivity,该activity的布局文件很简单
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:fitsSystemWindows="true"
android:id="@+id/rl_beijing"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.smarttrust.hunlian.UI.TitlebarUI
android:id="@+id/titlebarUI"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:background="@drawable/morenbeijing"
android:layout_below="@id/titlebarUI"
android:id="@+id/ec_layout_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
然后,自己写一个ChatFragment,继承EaseChatFragment,在ChatRoomActivity中
private FrameLayout ec_layout_container;
private EaseChatFragment chatFragment;
private void initChat(){
///调用EaseUI封装好的聊天页面
chatFragment = new ChatFragment();
///将参数传递给聊天页面
chatFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().
add(R.id.ec_layout_container,chatFragment).commit();
}
到此就实现了简单的聊天功能,继续往下
聊天肯定要有头像,昵称,点击头像触发跳转(比如跳转到该人的主页),点击消息内容触发(删除该条消息,或者长按复制)
1,先说说怎么传头像,这里只要上传自己的头像然后对方接收即可,这里又回到上面的方法,在ChatRoomActivity中
private void initChat(){
SharedPreferences preferences=getSharedPreferences("UserMessage", Context.MODE_PRIVATE);
String headurl=preferences.getString("headurl", "");//头像
String userNickname=preferences.getString("nickname", "");//昵称
///调用EaseUI封装好的聊天页面
chatFragment = new ChatFragment();
chatFragment.setSetmobile(setmobile);
chatFragment.setUserNickname(userNickname);
chatFragment.setHearurl(headurl);
// chatFragment.setUrlBg(drawable);
///将参数传递给聊天页面
chatFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().
add(R.id.ec_layout_container,chatFragment).commit();
}
我这里在环信的EaseChatFragment中定义了昵称,头像的set,get方法,获取ChatRoomActivity中传过来的昵称和头像,扩展EMMessage,比如在发送文本,图片,位置,语音的地方扩展,找到如下方法进行拓展即可,代码如下
//send message
protected void sendTextMessage(String content) {
if(EaseAtMessageHelper.get().containsAtUsername(content)){
sendAtMessage(content);
}else{
EMMessage message = EMMessage.createTxtSendMessage(content, toChatUsername);
message.setAttribute("headurl",getHearurl());
message.setAttribute("userNickname",getUserNickname());
sendMessage(message);
Log.e("sujd===========send",getHearurl());
Log.e("sujd===========send",getUserNickname());
}
}
protected void sendBigExpressionMessage(String name, String identityCode){
EMMessage message = EaseCommonUtils.createExpressionMessage(toChatUsername, name, identityCode);
message.setAttribute("headurl",getHearurl());
message.setAttribute("userNickname",getUserNickname());
sendMessage(message);
}
protected void sendVoiceMessage(String filePath, int length) {
EMMessage message = EMMessage.createVoiceSendMessage(filePath, length, toChatUsername);
message.setAttribute("headurl",getHearurl());
message.setAttribute("userNickname",getUserNickname());
sendMessage(message);
}
protected void sendImageMessage(String imagePath) {
EMMessage message = EMMessage.createImageSendMessage(imagePath, false, toChatUsername);
message.setAttribute("headurl",getHearurl());
message.setAttribute("userNickname",getUserNickname());
sendMessage(message);
}
protected void sendLocationMessage(double latitude, double longitude, String locationAddress) {
EMMessage message = EMMessage.createLocationSendMessage(latitude, longitude, locationAddress, toChatUsername);
message.setAttribute("headurl",getHearurl());
message.setAttribute("userNickname",getUserNickname());
sendMessage(message);
}
protected void sendVideoMessage(String videoPath, String thumbPath, int videoLength) {
EMMessage message = EMMessage.createVideoSendMessage(videoPath, thumbPath, videoLength, toChatUsername);
message.setAttribute("headurl",getHearurl());
message.setAttribute("userNickname",getUserNickname());
sendMessage(message);
}
protected void sendFileMessage(String filePath) {
EMMessage message = EMMessage.createFileSendMessage(filePath, toChatUsername);
message.setAttribute("headurl",getHearurl());
message.setAttribute("userNickname",getUserNickname());
sendMessage(message);
}
发送以后得接受显示出来,找到EaseChatRow这个类,找到方法修改即可
private void setUpBaseView() {
// set nickname, avatar and background of bubble
TextView timestamp = (TextView) findViewById(R.id.timestamp);
if (timestamp != null) {
if (position == 0) {
timestamp.setText(DateUtils.getTimestampString(new Date(message.getMsgTime())));
timestamp.setVisibility(View.VISIBLE);
} else {
// show time stamp if interval with last message is > 30 seconds
EMMessage prevMessage = (EMMessage) adapter.getItem(position - 1);
if (prevMessage != null && DateUtils.isCloseEnough(message.getMsgTime(), prevMessage.getMsgTime())) {
timestamp.setVisibility(View.GONE);
} else {
timestamp.setText(DateUtils.getTimestampString(new Date(message.getMsgTime())));
timestamp.setVisibility(View.VISIBLE);
}
}
}
if (userAvatarView != null) {
//set nickname and avatar
try {
Log.e("环信=======getFrom", message.getStringAttribute("headurl"));
} catch (HyphenateException e) {
e.printStackTrace();
}
if (message.direct() == Direct.SEND) {
try {
EaseUserUtils.setUserAvatar(context, message.getStringAttribute("headurl"), EMClient.getInstance().getCurrentUser(), userAvatarView);
} catch (HyphenateException e) {
e.printStackTrace();
}
} else {
try {
EaseUserUtils.setUserAvatar(context, message.getStringAttribute("headurl"), message.getFrom(), userAvatarView);
} catch (HyphenateException e) {
e.printStackTrace();
}
EaseUserUtils.setUserNick(message.getFrom(), usernickView);
}
}
if (deliveredView != null) {
if (message.isDelivered()) {
deliveredView.setVisibility(View.VISIBLE);
} else {
deliveredView.setVisibility(View.INVISIBLE);
}
}
if (ackedView != null) {
if (message.isAcked()) {
if (deliveredView != null) {
deliveredView.setVisibility(View.INVISIBLE);
}
ackedView.setVisibility(View.VISIBLE);
} else {
ackedView.setVisibility(View.INVISIBLE);
}
}
if (itemStyle != null) {
if (userAvatarView != null) {
if (itemStyle.isShowAvatar()) {
userAvatarView.setVisibility(View.VISIBLE);
EaseAvatarOptions avatarOptions = EaseUI.getInstance().getAvatarOptions();
if (avatarOptions != null && userAvatarView instanceof EaseImageView) {
EaseImageView avatarView = ((EaseImageView) userAvatarView);
if (avatarOptions.getAvatarShape() != 0)
avatarView.setShapeType(avatarOptions.getAvatarShape());
if (avatarOptions.getAvatarBorderWidth() != 0)
avatarView.setBorderWidth(avatarOptions.getAvatarBorderWidth());
if (avatarOptions.getAvatarBorderColor() != 0)
avatarView.setBorderColor(avatarOptions.getAvatarBorderColor());
if (avatarOptions.getAvatarRadius() != 0)
avatarView.setRadius(avatarOptions.getAvatarRadius());
}
} else {
userAvatarView.setVisibility(View.GONE);
}
}
if (usernickView != null) {
if (itemStyle.isShowUserNick())
usernickView.setVisibility(View.VISIBLE);
else
usernickView.setVisibility(View.GONE);
}
if (bubbleLayout != null) {
if (message.direct() == Direct.SEND) {
if (itemStyle.getMyBubbleBg() != null) {
bubbleLayout.setBackgroundDrawable(((EaseMessageAdapter) adapter).getMyBubbleBg());
}
} else if (message.direct() == Direct.RECEIVE) {
if (itemStyle.getOtherBubbleBg() != null) {
bubbleLayout.setBackgroundDrawable(((EaseMessageAdapter) adapter).getOtherBubbleBg());
}
}
}
}
}
有时候需要把头像设置成圆形,找到上面方法用到的工具类EaseUserUtils,进行修改
public static void setUserAvatar(Context context,String hearurl, String username, ImageView imageView){
EaseUser user = getUserInfo(username);
Log.e("环信=======username",username);
Log.e("环信=======username",user+"");
//头像设置为圆形
Glide.with(context).load(hearurl)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.bitmapTransform(new CropCircleTransformation(context))
.into(imageView);
}
/**
* set user's nickname
*/
public static void setUserNick(String username,TextView textView){
if(textView != null){
EaseUser user = getUserInfo(username);
if(user != null && user.getNick() != null){
textView.setText(user.getNick());
}else{
textView.setText(username);
}
}
}
到此就完成了头像,昵称的设置,接下来看点击事件
ChatFragment extends EaseChatFragment implements EaseChatFragmentHelper
在我们的这个类里实现如下方法,进行相应的点击 即可
@Override
public void onSetMessageAttributes(EMMessage message) {
Log.e("sujd==========","11111");
}
@Override
public void onEnterToChatDetails() {
Log.e("sujd==========","22222");
}
@Override
public void onAvatarClick(String username) {
Log.e("sujd==========","点击头像触发的事件");
}
@Override
public void onAvatarLongClick(String username) {
Log.e("sujd==========","44444");
}
@Override
public boolean onMessageBubbleClick(EMMessage message) {
Log.e("sujd==========","55555");
return false;
}
@Override
public void onMessageBubbleLongClick(final EMMessage message) {
Log.e("sujd==========","消息框长按事件");
}
@Override
public boolean onExtendMenuItemClick(int itemId, View view) {
Log.e("sujd==========","77777");
return false;
}
@Override
public EaseCustomChatRowProvider onSetCustomChatRowProvider() {
Log.e("sujd==========","88888");
return null;
}
}