实训第六周(1)

从本周开始,我开始写P2P聊天界面相关的内容,主要用的是网易云通信相关的云服务。

本次主要实现用网易云服务实现文本,图片,语音,地理位置等信息的创建和发送,同时实现历史消息记录的加载。

1.首先需要关联ChatSession类得到聊天对象的账号:

private Context mContext;
    private ChatSession mChatSession;

    public ChatMsgHandler(Context context, ChatSession session) {
        mContext = context;
        mChatSession = session;
    }

2.创建文本消息:

public IMMessage createTextMessage(String text) {
        // 创建文本消息
        return MessageBuilder.createTextMessage(mChatSession.getChatAccount(),
                mChatSession.getSessionType(), text);
    }

3.创建图片消息:消息内容为图片保存路径。

public IMMessage createImageMessage(String path) {
        return MessageBuilder.createImageMessage(mChatSession.getSessionId(),
                mChatSession.getSessionType(), new File(path));
    }

4.创建语音消息:消息内容为语音保存路径和语音时长。

public IMMessage createAudioMessage(String path, long time) {
        return MessageBuilder.createAudioMessage(mChatSession.getSessionId(),
                mChatSession.getSessionType(), new File(path), time);
    }

5.创建地理位置信息:内容为经纬度和位置文字描述

public IMMessage createLocMessage(LatLonPoint latLonPoint, String address) {
        return MessageBuilder.createLocationMessage(mChatSession.getSessionId(),
                mChatSession.getSessionType(), latLonPoint.getLatitude(), latLonPoint.getLongitude(),
                address);
    }

6.加载历史消息记录:加载成功会得到一个放IMMessage的List-result

public void loadMessage(final IMMessage anchorMessage, final OnLoadMsgListener listener) {
        NIMClient.getService(MsgService.class).queryMessageListEx(anchorMessage,
                QueryDirectionEnum.QUERY_OLD, ONE_QUERY_LIMIT, true)
                .setCallback(new RequestCallbackWrapper<List<IMMessage>>() {
                    @Override
                    public void onResult(int code, List<IMMessage> result, Throwable exception) {
                        if (exception != null) {
                            listener.loadFail(exception.getMessage());
                            return;
                        }
                        if (code != 200) {
                            listener.loadFail("code:" + code);
                            return;
                        }

                        listener.loadSuccess(result, anchorMessage);

                    }
                });
    }

OnLoadMsgListener

public interface OnLoadMsgListener {
        void loadSuccess(List<IMMessage> messages, IMMessage anchorMessage);

        void loadFail(String message);
    }

可以在具体显示消息的activity中重写,实现具体的功能。

7.处理历史消息记录,如果两条消息之间相隔大于TEN_MINUTE,则需要在两条之间新增时间点文本消息。

 public List<IMMessage> dealLoadMessage(List<IMMessage> messages, IMMessage anchorMessage) {
        IMMessage lastMsg = messages.get(messages.size() - 1);
        if (anchorMessage.getTime() - lastMsg.getTime() >= TEN_MINUTE) {
            messages.add(messages.size() - 1,createTimeMessage(lastMsg));
        }

        for (int i = messages.size() - 2; i > 0; i--) {
            if (!TextUtils.isEmpty(messages.get(i).getUuid()) &&
                    !TextUtils.isEmpty(messages.get(i-1).getUuid())){
                if (messages.get(i).getTime() - messages.get(i-1).getTime() >= TEN_MINUTE) {
                    messages.add(i , createTimeMessage(messages.get(i)));
                }
            }
        }

        return messages;
    }


    public IMMessage createTimeMessage(IMMessage message) {
        return MessageBuilder.createEmptyMessage(message.getSessionId(),
                message.getSessionType(), message.getTime());
    }

8.在此过程中用到的ChatSession类:主要用于属性等信息的保存和获取。

public class ChatSession {

    private SessionTypeEnum mSessionType;
    private String mSessionId;
    private String mMyAccount;
    private String mChatAccount;
    private String mChatNick;
    private NimUserInfo mMyInfo;
    private NimUserInfo mChatInfo;

    public SessionTypeEnum getSessionType() {
        return mSessionType;
    }

    public void setSessionType(SessionTypeEnum sessionType) {
        mSessionType = sessionType;
    }

    public String getSessionId() {
        return mSessionId;
    }

    public void setSessionId(String sessionId) {
        mSessionId = sessionId;
    }

    public String getMyAccount() {
        return mMyAccount;
    }

    public void setMyAccount(String myAccount) {
        mMyAccount = myAccount;
    }

    public String getChatAccount() {
        return mChatAccount;
    }


    public void setChatAccount(String chatAccount) {
        mChatAccount = chatAccount;
    }

    public String getChatNick() {
        return mChatNick;
    }

    public void setChatNick(String chatNick) {
        mChatNick = chatNick;
    }

    public NimUserInfo getMyInfo() {
        return mMyInfo;
    }

    public void setMyInfo(NimUserInfo myInfo) {
        mMyInfo = myInfo;
    }

    public NimUserInfo getChatInfo() {
        return mChatInfo;
    }

    public void setChatInfo(NimUserInfo chatInfo) {
        mChatInfo = chatInfo;
    }
}


没有更多推荐了,返回首页