需要在聊天中加入自定义的消息,比如旅游图片,旅游景点,等等,这个时候融云集成好的一些消息体已经不够用,官方文档上已经说明了如何去自定义,当然啦,相信你们肯定会先找一找有没有现成写好的,抱歉,你还是看文档吧,这里我写一下我遇到的坑,拿出一个我写的例子:
如同集成文档一样,需要先自定义消息体
package com.xxx.xxx.CustomizeRongIM;
import android.os.Parcel;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import io.rong.common.ParcelUtils;
import io.rong.common.RLog;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MentionedInfo;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.UserInfo;
/**
* Created by Obl on 2017/5/9.
* com.xxx.xxx.CustomizeRongIM
*/
@MessageTag(value = "XX:XXXXX", flag = MessageTag.ISCOUNTED | MessageTag.ISPERSISTED)
public class CustomizeJoinMessage extends MessageContent {
public CustomizeJoinMessage() {
}
//给消息赋值。
public CustomizeJoinMessage(Parcel in) {
setUserInfo(ParcelUtils.readFromParcel(in, UserInfo.class));
setMentionedInfo(ParcelUtils.readFromParcel(in, MentionedInfo.class));
}
/**
* 将类的数据写入外部提供的 Parcel 中。
*
* @param dest 对象被写入的 Parcel。
* @param flags 对象如何被写入的附加标志。
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
ParcelUtils.writeToParcel(dest, this.getUserInfo());
ParcelUtils.writeToParcel(dest, this.getMentionedInfo());
}
public CustomizeJoinMessage(byte[] data) {
String jsonStr = null;
try {
jsonStr = new String(data, "UTF-8");
} catch (UnsupportedEncodingException var5) {
var5.printStackTrace();
}
try {
JSONObject e = new JSONObject(jsonStr);
if (e.has("user")) {
this.setUserInfo(this.parseJsonToUserInfo(e.getJSONObject("user")));
}
if (e.has("mentionedInfo")) {
this.setMentionedInfo(this.parseJsonToMentionInfo(e.getJSONObject("mentionedInfo")));
}
} catch (JSONException var4) {
RLog.e("CustomizeJoinMessage", jsonStr + "JSONException " + var4.getMessage());
}
}
/**
* 读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。
*/
public static final Creator<CustomizeJoinMessage> CREATOR = new Creator<CustomizeJoinMessage>() {
@Override
public CustomizeJoinMessage createFromParcel(Parcel source) {
return new CustomizeJoinMessage(source);
}
@Override
public CustomizeJoinMessage[] newArray(int size) {
return new CustomizeJoinMessage[size];
}
};
/**
* 描述了包含在 Parcelable 对象排列信息中的特殊对象的类型。
*
* @return 一个标志位,表明Parcelable对象特殊对象类型集合的排列。
*/
public int describeContents() {
return 0;
}
@Override
public byte[] encode() {
JSONObject jsonObj = new JSONObject();
try {
if (this.getJSONUserInfo() != null) {
jsonObj.putOpt("user", this.getJSONUserInfo());
}
if (this.getJsonMentionInfo() != null) {
jsonObj.putOpt("mentionedInfo", this.getJsonMentionInfo());
}
} catch (JSONException var4) {
RLog.e("CustomizeJoinMessage", "JSONException " + var4.getMessage());
}
try {
return jsonObj.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException var3) {
var3.printStackTrace();
return null;
}
}
}
这里需要注意的是enCode 和 带有byte 的构造方法一定要写,,查看源码你会发现每个消息都含有这几个方法。
package com.xxx.xxx.CustomizeRongIM;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.xxx.xxx.R;
import com.xxx.xxx.event.AddNewOneEvent;
import com.xxx.xxx.utils.DensityUtils;
import org.greenrobot.eventbus.EventBus;
import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.widget.provider.IContainerItemProvider;
/**
* Created by Obl on 2017/5/9.
* com.xxx.xxx.CustomizeRongIM
*/
@ProviderTag(messageContent = CustomizeJoinMessage.class, showPortrait = false, centerInHorizontal = true, showSummaryWithName = false)
public class CustomizeJoinMessageItemProvider extends IContainerItemProvider.MessageProvider<CustomizeJoinMessage> {
class ViewHolder {
LinearLayout llJoin;
}
@Override
public View newView(Context context, ViewGroup group) {
View view = LayoutInflater.from(context).inflate(R.layout.item_customize_join_message, group, false);
ViewHolder holder = new ViewHolder();
holder.llJoin = (LinearLayout) view.findViewById(R.id.llJoin);
holder.llJoin.getLayoutParams().width = DensityUtils.getScreenWidth(context);
view.setTag(holder);
return view;
}
@Override
public void bindView(View view, int position, CustomizeJoinMessage content, UIMessage message) {
}
@Override
public Spannable getContentSummary(CustomizeJoinMessage data) {
return new SpannableString("创建家庭组成功");
}
@Override
public void onItemClick(View view, int i, CustomizeJoinMessage customizeBPMessage, UIMessage uiMessage) {
EventBus.getDefault().post(new AddNewOneEvent());
}
@Override
public void onItemLongClick(View view, int i, CustomizeJoinMessage customizeBPMessage, UIMessage uiMessage) {
}
}
如果你比较懒,,你可以去看看
这里已经写好了很多原生的,,你可以修修改改,,
对了,别忘记注册在init()之后