com.easemob.chat.TextMessageBody Java Examples

The following examples show how to use com.easemob.chat.TextMessageBody. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChatActivity.java    From school_shop with MIT License 6 votes vote down vote up
/**
 * 发送文本消息
 * 
 * @param content
 *            message content
 * @param isResend
 *            boolean resend
 */
private void sendText(String content) {

	if (content.length() > 0) {
		EMMessage message = EMMessage.createSendMessage(EMMessage.Type.TXT);
		// 如果是群聊,设置chattype,默认是单聊
		if (chatType == CHATTYPE_GROUP)
			message.setChatType(ChatType.GroupChat);
		TextMessageBody txtBody = new TextMessageBody(content);
		// 设置消息body
		message.addBody(txtBody);
		// 设置要发给谁,用户username或者群聊groupid
		message.setReceipt(toChatUsername);
		// 把messgage加到conversation中
		conversation.addMessage(message);
		// 通知adapter有消息变动,adapter会根据加入的这条message显示消息和调用sdk的发送方法
		adapter.refreshSelectLast();
		mEditTextContent.setText("");

		setResult(RESULT_OK);

	}
}
 
Example #2
Source File: ChatActivity.java    From school_shop with MIT License 6 votes vote down vote up
/**
 * 转发消息
 * 
 * @param forward_msg_id
 */
protected void forwardMessage(String forward_msg_id) {
	EMMessage forward_msg = EMChatManager.getInstance().getMessage(forward_msg_id);
	EMMessage.Type type = forward_msg.getType();
	switch (type) {
	case TXT:
		// 获取消息内容,发送消息
		String content = ((TextMessageBody) forward_msg.getBody()).getMessage();
		sendText(content);
		break;
	case IMAGE:
		// 发送图片
		String filePath = ((ImageMessageBody) forward_msg.getBody()).getLocalUrl();
		if (filePath != null) {
			File file = new File(filePath);
			if (!file.exists()) {
				// 不存在大图发送缩略图
				filePath = ImageUtils.getThumbnailImagePath(filePath);
			}
			sendPicture(filePath);
		}
		break;
	default:
		break;
	}
}
 
Example #3
Source File: EaseChatFragment.java    From monolog-android with MIT License 5 votes vote down vote up
/**
 * 转发消息
 * 
 * @param forward_msg_id
 */
protected void forwardMessage(String forward_msg_id) {
    final EMMessage forward_msg = EMChatManager.getInstance().getMessage(forward_msg_id);
    EMMessage.Type type = forward_msg.getType();
    switch (type) {
    case TXT:
        // 获取消息内容,发送消息
        String content = ((TextMessageBody) forward_msg.getBody()).getMessage();
        sendTextMessage(content);
        break;
    case IMAGE:
        // 发送图片
        String filePath = ((ImageMessageBody) forward_msg.getBody()).getLocalUrl();
        if (filePath != null) {
            File file = new File(filePath);
            if (!file.exists()) {
                // 不存在大图发送缩略图
                filePath = EaseImageUtils.getThumbnailImagePath(filePath);
            }
            sendImageMessage(filePath);
        }
        break;
    default:
        break;
    }
    
    if(forward_msg.getChatType() == EMMessage.ChatType.ChatRoom){
        EMChatManager.getInstance().leaveChatRoom(forward_msg.getTo());
    }
}
 
Example #4
Source File: ChatActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 发送文本消息
 * 
 * @param content
 *            message content
 * @param isResend
 *            boolean resend
 */
private void sendText(String content) {

    if (content.length() > 0) {
        EMMessage message = EMMessage.createSendMessage(EMMessage.Type.TXT);
        // 如果是群聊,设置chattype,默认是单聊
        if (chatType == CHATTYPE_GROUP)
            message.setChatType(ChatType.GroupChat);
        TextMessageBody txtBody = new TextMessageBody(content);
        // 设置消息body
        message.addBody(txtBody);
        // 设置要发给谁,用户username或者群聊groupid
        message.setReceipt(toChatUsername);
        message.setAttribute("toUserNick", toUserNick);
        message.setAttribute("toUserAvatar", toUserAvatar);
        message.setAttribute("useravatar", myUserAvatar);
        message.setAttribute("usernick", myUserNick);
        // 把messgage加到conversation中
        conversation.addMessage(message);
        // 通知adapter有消息变动,adapter会根据加入的这条message显示消息和调用sdk的发送方法
        adapter.refresh();
        listView.setSelection(listView.getCount() - 1);
        mEditTextContent.setText("");

        setResult(RESULT_OK);

    }
}
 
Example #5
Source File: ChatActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 转发消息
 * 
 * @param forward_msg_id
 */
protected void forwardMessage(String forward_msg_id) {
    EMMessage forward_msg = EMChatManager.getInstance().getMessage(
            forward_msg_id);
    EMMessage.Type type = forward_msg.getType();
    switch (type) {
    case TXT:
        // 获取消息内容,发送消息
        String content = ((TextMessageBody) forward_msg.getBody())
                .getMessage();
        sendText(content);
        break;
    case IMAGE:
        // 发送图片
        String filePath = ((ImageMessageBody) forward_msg.getBody())
                .getLocalUrl();
        if (filePath != null) {
            File file = new File(filePath);
            if (!file.exists()) {
                // 不存在大图发送缩略图
                filePath = ImageUtils.getThumbnailImagePath(filePath);
            }
            sendPicture(filePath, false);
        }
        break;
    default:
        break;
    }
}
 
Example #6
Source File: MainActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onInvitationReceived(String groupId, String groupName,
        String inviter, String reason) {

    // 被邀请
    String st3 = getResources().getString(
            R.string.Invite_you_to_join_a_group_chat);
    User user = MYApplication.getInstance().getContactList()
            .get(inviter);
    if (user != null) {
        EMMessage msg = EMMessage.createReceiveMessage(Type.TXT);
        msg.setChatType(ChatType.GroupChat);
        msg.setFrom(inviter);
        msg.setTo(groupId);
        msg.setMsgId(UUID.randomUUID().toString());
        msg.addBody(new TextMessageBody(user.getNick() + st3));
        msg.setAttribute("useravatar", user.getAvatar());
        msg.setAttribute("usernick", user.getNick());
        // 保存邀请消息
        EMChatManager.getInstance().saveMessage(msg);
        // 提醒新消息
        EMNotifier.getInstance(getApplicationContext())
                .notifyOnNewMsg();
    }
    runOnUiThread(new Runnable() {
        public void run() {
            updateUnreadLabel();
            // 刷新ui
            if (currentTabIndex == 0)
                homefragment.refresh();
            // if (CommonUtils.getTopActivity(MainActivity.this).equals(
            // GroupsActivity.class.getName())) {
            // GroupsActivity.instance.onResume();
            // }
        }
    });

}
 
Example #7
Source File: MainActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onApplicationAccept(String groupId, String groupName,
        String accepter) {
    String st4 = getResources().getString(
            R.string.Agreed_to_your_group_chat_application);
    // 加群申请被同意
    EMMessage msg = EMMessage.createReceiveMessage(Type.TXT);
    msg.setChatType(ChatType.GroupChat);
    msg.setFrom(accepter);
    msg.setTo(groupId);
    msg.setMsgId(UUID.randomUUID().toString());
    msg.addBody(new TextMessageBody(accepter + st4));
    // 保存同意消息
    EMChatManager.getInstance().saveMessage(msg);
    // 提醒新消息
    EMNotifier.getInstance(getApplicationContext()).notifyOnNewMsg();

    runOnUiThread(new Runnable() {
        public void run() {
            updateUnreadLabel();
            // 刷新ui
            if (currentTabIndex == 0)
                homefragment.refresh();
            // if (CommonUtils.getTopActivity(MainActivity.this).equals(
            // GroupsActivity.class.getName())) {
            // GroupsActivity.instance.onResume();
            // }
        }
    });
}
 
Example #8
Source File: MessageAdapter.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 文本消息
 * 
 * @param message
 * @param holder
 * @param position
 */
private void handleTextMessage(EMMessage message, ViewHolder holder,
        final int position) {
    TextMessageBody txtBody = (TextMessageBody) message.getBody();
    Spannable span = SmileUtils
            .getSmiledText(context, txtBody.getMessage());
    // 设置内容
    holder.tv.setText(span, BufferType.SPANNABLE);
    // 设置长按事件监听
    // holder.tv.setOnLongClickListener(new OnLongClickListener() {
    // @Override
    // public boolean onLongClick(View v) {
    // activity.startActivityForResult((new Intent(activity,
    // ContextMenu.class)).putExtra("position", position)
    // .putExtra("type", EMMessage.Type.TXT.ordinal()),
    // ChatActivity.REQUEST_CODE_CONTEXT_MENU);
    // return true;
    // }
    // });

    if (message.direct == EMMessage.Direct.SEND) {
        switch (message.status) {
        case SUCCESS: // 发送成功
            holder.pb.setVisibility(View.GONE);
            holder.staus_iv.setVisibility(View.GONE);
            break;
        case FAIL: // 发送失败
            holder.pb.setVisibility(View.GONE);
            holder.staus_iv.setVisibility(View.VISIBLE);
            break;
        case INPROGRESS: // 发送中
            holder.pb.setVisibility(View.VISIBLE);
            holder.staus_iv.setVisibility(View.GONE);
            break;
        default:
            // 发送消息
            sendMsgInBackground(message, holder);
        }
    }
}
 
Example #9
Source File: MessageAdapter.java    From school_shop with MIT License 5 votes vote down vote up
/**
 * 文本消息
 * 
 * @param message
 * @param holder
 * @param position
 */
private void handleTextMessage(EMMessage message, ViewHolder holder, final int position) {
	TextMessageBody txtBody = (TextMessageBody) message.getBody();
	Spannable span = SmileUtils.getSmiledText(context, txtBody.getMessage());
	// 设置内容
	holder.tv.setText(span, BufferType.SPANNABLE);
	// 设置长按事件监听
	holder.tv.setOnLongClickListener(new OnLongClickListener() {
		@Override
		public boolean onLongClick(View v) {
			activity.startActivityForResult(
					(new Intent(activity, ContextMenu.class)).putExtra("position", position).putExtra("type",
							EMMessage.Type.TXT.ordinal()), ChatActivity.REQUEST_CODE_CONTEXT_MENU);
			return true;
		}
	});

	if (message.direct == EMMessage.Direct.SEND) {
		switch (message.status) {
		case SUCCESS: // 发送成功
			holder.pb.setVisibility(View.GONE);
			holder.staus_iv.setVisibility(View.GONE);
			break;
		case FAIL: // 发送失败
			holder.pb.setVisibility(View.GONE);
			holder.staus_iv.setVisibility(View.VISIBLE);
			break;
		case INPROGRESS: // 发送中
			holder.pb.setVisibility(View.VISIBLE);
			holder.staus_iv.setVisibility(View.GONE);
			break;
		default:
			// 发送消息
			sendMsgInBackground(message, holder);
		}
	}
}
 
Example #10
Source File: EaseChatRowText.java    From monolog-android with MIT License 4 votes vote down vote up
@Override
    public void onSetUpView() {
        TextMessageBody txtBody = (TextMessageBody) message.getBody();
        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
        // 设置内容
        contentView.setText(span, BufferType.SPANNABLE);

        if (message.direct == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status) {
            case CREATE: 
                progressBar.setVisibility(View.VISIBLE);
                statusView.setVisibility(View.GONE);
                // 发送消息
//                sendMsgInBackground(message);
                break;
            case SUCCESS: // 发送成功
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.GONE);
                break;
            case FAIL: // 发送失败
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.VISIBLE);
                break;
            case INPROGRESS: // 发送中
                progressBar.setVisibility(View.VISIBLE);
                statusView.setVisibility(View.GONE);
                break;
            default:
               break;
            }
        }else{
            if(!message.isAcked() && message.getChatType() == ChatType.Chat){
                try {
                    EMChatManager.getInstance().ackMessageRead(message.getFrom(), message.getMsgId());
                    message.isAcked = true;
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
Example #11
Source File: MessageAdapter.java    From FanXin-based-HuanXin with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 音视频通话记录
 * 
 * @param message
 * @param holder
 * @param position
 */
private void handleCallMessage(EMMessage message, ViewHolder holder,
        final int position) {
    TextMessageBody txtBody = (TextMessageBody) message.getBody();
    holder.tv.setText(txtBody.getMessage());

}
 
Example #12
Source File: MessageAdapter.java    From school_shop with MIT License 2 votes vote down vote up
/**
 * 音视频通话记录
 * 
 * @param message
 * @param holder
 * @param position
 */
private void handleCallMessage(EMMessage message, ViewHolder holder, final int position) {
	TextMessageBody txtBody = (TextMessageBody) message.getBody();
	holder.tv.setText(txtBody.getMessage());

}