Java Code Examples for com.easemob.chat.EMMessage#addBody()
The following examples show how to use
com.easemob.chat.EMMessage#addBody() .
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 FanXin-based-HuanXin with GNU General Public License v2.0 | 6 votes |
/** * 发送位置信息 * * @param latitude * @param longitude * @param imagePath * @param locationAddress */ private void sendLocationMsg(double latitude, double longitude, String imagePath, String locationAddress) { EMMessage message = EMMessage .createSendMessage(EMMessage.Type.LOCATION); // 如果是群聊,设置chattype,默认是单聊 if (chatType == CHATTYPE_GROUP) message.setChatType(ChatType.GroupChat); LocationMessageBody locBody = new LocationMessageBody(locationAddress, latitude, longitude); message.addBody(locBody); message.setReceipt(toChatUsername); message.setAttribute("toUserNick", toUserNick); message.setAttribute("toUserAvatar", toUserAvatar); message.setAttribute("useravatar", myUserAvatar); message.setAttribute("usernick", myUserNick); conversation.addMessage(message); listView.setAdapter(adapter); adapter.notifyDataSetChanged(); listView.setSelection(listView.getCount() - 1); setResult(RESULT_OK); }
Example 2
Source File: ChatActivity.java From school_shop with MIT License | 6 votes |
/** * 发送文本消息 * * @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 3
Source File: ChatActivity.java From school_shop with MIT License | 6 votes |
/** * 发送语音 * * @param filePath * @param fileName * @param length * @param isResend */ private void sendVoice(String filePath, String fileName, String length, boolean isResend) { if (!(new File(filePath).exists())) { return; } try { final EMMessage message = EMMessage.createSendMessage(EMMessage.Type.VOICE); // 如果是群聊,设置chattype,默认是单聊 if (chatType == CHATTYPE_GROUP) message.setChatType(ChatType.GroupChat); message.setReceipt(toChatUsername); int len = Integer.parseInt(length); VoiceMessageBody body = new VoiceMessageBody(new File(filePath), len); message.addBody(body); conversation.addMessage(message); adapter.refreshSelectLast(); setResult(RESULT_OK); // send file // sendVoiceSub(filePath, fileName, message); } catch (Exception e) { e.printStackTrace(); } }
Example 4
Source File: ChatActivity.java From school_shop with MIT License | 6 votes |
/** * 发送图片 * * @param filePath */ private void sendPicture(final String filePath) { String to = toChatUsername; // create and add image message in view final EMMessage message = EMMessage.createSendMessage(EMMessage.Type.IMAGE); // 如果是群聊,设置chattype,默认是单聊 if (chatType == CHATTYPE_GROUP) message.setChatType(ChatType.GroupChat); message.setReceipt(to); ImageMessageBody body = new ImageMessageBody(new File(filePath)); // 默认超过100k的图片会压缩后发给对方,可以设置成发送原图 // body.setSendOriginalImage(true); message.addBody(body); conversation.addMessage(message); listView.setAdapter(adapter); adapter.refreshSelectLast(); setResult(RESULT_OK); // more(more); }
Example 5
Source File: ChatActivity.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 5 votes |
/** * 发送文本消息 * * @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 6
Source File: ChatActivity.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 5 votes |
/** * 发送语音 * * @param filePath * @param fileName * @param length * @param isResend */ private void sendVoice(String filePath, String fileName, String length, boolean isResend) { if (!(new File(filePath).exists())) { return; } try { final EMMessage message = EMMessage .createSendMessage(EMMessage.Type.VOICE); // 如果是群聊,设置chattype,默认是单聊 if (chatType == CHATTYPE_GROUP) message.setChatType(ChatType.GroupChat); message.setReceipt(toChatUsername); message.setAttribute("toUserNick", toUserNick); message.setAttribute("toUserAvatar", toUserAvatar); message.setAttribute("useravatar", myUserAvatar); message.setAttribute("usernick", myUserNick); int len = Integer.parseInt(length); VoiceMessageBody body = new VoiceMessageBody(new File(filePath), len); message.addBody(body); conversation.addMessage(message); adapter.refresh(); listView.setSelection(listView.getCount() - 1); setResult(RESULT_OK); // send file // sendVoiceSub(filePath, fileName, message); } catch (Exception e) { e.printStackTrace(); } }
Example 7
Source File: ChatActivity.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 5 votes |
/** * 发送图片 * * @param filePath */ private void sendPicture(final String filePath, boolean is_share) { Log.e("filePath------>>>>", filePath); String to = toChatUsername; // create and add image message in view final EMMessage message = EMMessage .createSendMessage(EMMessage.Type.IMAGE); // 如果是群聊,设置chattype,默认是单聊 if (chatType == CHATTYPE_GROUP) message.setChatType(ChatType.GroupChat); message.setReceipt(to); message.setAttribute("toUserNick", toUserNick); message.setAttribute("toUserAvatar", toUserAvatar); message.setAttribute("useravatar", myUserAvatar); message.setAttribute("usernick", myUserNick); if (is_share) { message.setAttribute("isShare", "yes"); } ImageMessageBody body = new ImageMessageBody(new File(filePath)); // 默认超过100k的图片会压缩后发给对方,可以设置成发送原图 // body.setSendOriginalImage(true); message.addBody(body); conversation.addMessage(message); listView.setAdapter(adapter); adapter.refresh(); listView.setSelection(listView.getCount() - 1); setResult(RESULT_OK); // more(more); }
Example 8
Source File: ChatActivity.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 5 votes |
/** * 发送视频消息 */ private void sendVideo(final String filePath, final String thumbPath, final int length) { final File videoFile = new File(filePath); if (!videoFile.exists()) { return; } try { EMMessage message = EMMessage .createSendMessage(EMMessage.Type.VIDEO); // 如果是群聊,设置chattype,默认是单聊 if (chatType == CHATTYPE_GROUP) message.setChatType(ChatType.GroupChat); String to = toChatUsername; message.setReceipt(to); message.setAttribute("toUserNick", toUserNick); message.setAttribute("toUserAvatar", toUserAvatar); message.setAttribute("useravatar", myUserAvatar); message.setAttribute("usernick", myUserNick); VideoMessageBody body = new VideoMessageBody(videoFile, thumbPath, length, videoFile.length()); message.addBody(body); conversation.addMessage(message); listView.setAdapter(adapter); adapter.refresh(); listView.setSelection(listView.getCount() - 1); setResult(RESULT_OK); } catch (Exception e) { e.printStackTrace(); } }
Example 9
Source File: MainActivity.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 5 votes |
@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 10
Source File: MainActivity.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 5 votes |
@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 11
Source File: ChatActivity.java From FanXin-based-HuanXin with GNU General Public License v2.0 | 4 votes |
/** * 发送文件 * * @param uri */ private void sendFile(Uri uri) { String filePath = null; if ("content".equalsIgnoreCase(uri.getScheme())) { String[] projection = { "_data" }; Cursor cursor = null; try { cursor = getContentResolver().query(uri, projection, null, null, null); int column_index = cursor.getColumnIndexOrThrow("_data"); if (cursor.moveToFirst()) { filePath = cursor.getString(column_index); } } catch (Exception e) { e.printStackTrace(); } } else if ("file".equalsIgnoreCase(uri.getScheme())) { filePath = uri.getPath(); } File file = new File(filePath); if (file == null || !file.exists()) { Toast.makeText(getApplicationContext(), "文件不存在", Toast.LENGTH_SHORT) .show(); return; } if (file.length() > 10 * 1024 * 1024) { Toast.makeText(getApplicationContext(), "文件不能大于10M", Toast.LENGTH_SHORT).show(); return; } // 创建一个文件消息 EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE); // 如果是群聊,设置chattype,默认是单聊 if (chatType == CHATTYPE_GROUP) message.setChatType(ChatType.GroupChat); message.setReceipt(toChatUsername); // add message body NormalFileMessageBody body = new NormalFileMessageBody(new File( filePath)); message.addBody(body); message.setAttribute("toUserNick", toUserNick); message.setAttribute("toUserAvatar", toUserAvatar); message.setAttribute("useravatar", myUserAvatar); message.setAttribute("usernick", myUserNick); conversation.addMessage(message); listView.setAdapter(adapter); adapter.refresh(); listView.setSelection(listView.getCount() - 1); setResult(RESULT_OK); }
Example 12
Source File: ChatActivity.java From school_shop with MIT License | 4 votes |
/** * 发送文件 * * @param uri */ private void sendFile(Uri uri) { String filePath = null; if ("content".equalsIgnoreCase(uri.getScheme())) { String[] projection = { "_data" }; Cursor cursor = null; try { cursor = getContentResolver().query(uri, projection, null, null, null); int column_index = cursor.getColumnIndexOrThrow("_data"); if (cursor.moveToFirst()) { filePath = cursor.getString(column_index); } } catch (Exception e) { e.printStackTrace(); } } else if ("file".equalsIgnoreCase(uri.getScheme())) { filePath = uri.getPath(); } File file = new File(filePath); if (file == null || !file.exists()) { String st7 = getResources().getString(R.string.File_does_not_exist); Toast.makeText(getApplicationContext(), st7, 0).show(); return; } if (file.length() > 10 * 1024 * 1024) { String st6 = getResources().getString(R.string.The_file_is_not_greater_than_10_m); Toast.makeText(getApplicationContext(), st6, 0).show(); return; } // 创建一个文件消息 EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE); // 如果是群聊,设置chattype,默认是单聊 if (chatType == CHATTYPE_GROUP) message.setChatType(ChatType.GroupChat); message.setReceipt(toChatUsername); // add message body NormalFileMessageBody body = new NormalFileMessageBody(new File(filePath)); message.addBody(body); conversation.addMessage(message); listView.setAdapter(adapter); adapter.refreshSelectLast(); setResult(RESULT_OK); }