Search in sources :

Example 6 with EMMessage

use of com.easemob.chat.EMMessage in project wechat by motianhuo.

the class App method initEMChat.

private void initEMChat() {
    int pid = android.os.Process.myPid();
    String processAppName = getAppName(pid);
    if (processAppName == null || !processAppName.equalsIgnoreCase("com.juns.wechat")) {
        return;
    }
    EMChatOptions options = EMChatManager.getInstance().getChatOptions();
    // 获取到EMChatOptions对象
    // 设置自定义的文字提示
    options.setNotifyText(new OnMessageNotifyListener() {

        @Override
        public String onNewMessageNotify(EMMessage message) {
            return "你的好友发来了一条消息哦";
        }

        @Override
        public String onLatestMessageNotify(EMMessage message, int fromUsersNum, int messageNum) {
            return fromUsersNum + "个好友,发来了" + messageNum + "条消息";
        }

        @Override
        public String onSetNotificationTitle(EMMessage arg0) {
            return null;
        }

        @Override
        public int onSetSmallIcon(EMMessage arg0) {
            return 0;
        }
    });
    options.setOnNotificationClickListener(new OnNotificationClickListener() {

        @Override
        public Intent onNotificationClick(EMMessage message) {
            Intent intent = new Intent(_context, MainActivity.class);
            ChatType chatType = message.getChatType();
            if (chatType == ChatType.Chat) {
                // 单聊信息
                intent.putExtra("userId", message.getFrom());
                intent.putExtra("chatType", ChatActivity.CHATTYPE_SINGLE);
            } else {
                // 群聊信息
                // message.getTo()为群聊id
                intent.putExtra("groupId", message.getTo());
                intent.putExtra("chatType", ChatActivity.CHATTYPE_GROUP);
            }
            return intent;
        }
    });
// IntentFilter callFilter = new
// IntentFilter(EMChatManager.getInstance()
// .getIncomingCallBroadcastAction());
// registerReceiver(new CallReceiver(), callFilter);
}
Also used : ChatType(com.easemob.chat.EMMessage.ChatType) OnMessageNotifyListener(com.easemob.chat.OnMessageNotifyListener) OnNotificationClickListener(com.easemob.chat.OnNotificationClickListener) Intent(android.content.Intent) EMChatOptions(com.easemob.chat.EMChatOptions) EMMessage(com.easemob.chat.EMMessage)

Example 7 with EMMessage

use of com.easemob.chat.EMMessage in project wechat by motianhuo.

the class NewMsgAdpter method getView.

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.layout_item_msg, parent, false);
    }
    ImageView img_avar = ViewHolder.get(convertView, R.id.contactitem_avatar_iv);
    TextView txt_name = ViewHolder.get(convertView, R.id.txt_name);
    TextView txt_state = ViewHolder.get(convertView, R.id.txt_state);
    TextView txt_del = ViewHolder.get(convertView, R.id.txt_del);
    TextView txt_content = ViewHolder.get(convertView, R.id.txt_content);
    TextView txt_time = ViewHolder.get(convertView, R.id.txt_time);
    TextView unreadLabel = ViewHolder.get(convertView, R.id.unread_msg_number);
    SwipeLayout swipe = ViewHolder.get(convertView, R.id.swipe);
    if (PublicMsg != null && position == 0) {
        txt_name.setText("订阅号");
        img_avar.setImageResource(R.drawable.icon_public);
        txt_time.setText(PublicMsg.getTime());
        txt_content.setText(PublicMsg.getContent());
        unreadLabel.setText("3");
        unreadLabel.setVisibility(View.VISIBLE);
        swipe.setSwipeEnabled(false);
    } else {
        swipe.setSwipeEnabled(true);
        // 获取与此用户/群组的会话
        final EMConversation conversation = conversationList.get(position);
        // 获取用户username或者群组groupid
        ChatID = conversation.getUserName();
        txt_del.setTag(ChatID);
        if (conversation.isGroup()) {
            img_avar.setImageResource(R.drawable.defult_group);
            GroupInfo info = GloableParams.GroupInfos.get(ChatID);
            if (info != null) {
                txt_name.setText(info.getGroup_name());
            } else {
            // initGroupInfo(img_avar, txt_name);// 获取群组信息
            }
        } else {
            User user = GloableParams.Users.get(ChatID);
            if (user != null) {
                txt_name.setText(user.getUserName());
            } else {
                txt_name.setText("好友");
                // 获取用户信息
                UserUtils.initUserInfo(context, ChatID, img_avar, txt_name);
            }
        }
        if (conversation.getUnreadMsgCount() > 0) {
            // 显示与此用户的消息未读数
            unreadLabel.setText(String.valueOf(conversation.getUnreadMsgCount()));
            unreadLabel.setVisibility(View.VISIBLE);
        } else {
            unreadLabel.setVisibility(View.INVISIBLE);
        }
        if (conversation.getMsgCount() != 0) {
            // 把最后一条消息的内容作为item的message内容
            EMMessage lastMessage = conversation.getLastMessage();
            txt_content.setText(SmileUtils.getSmiledText(context, getMessageDigest(lastMessage, context)), BufferType.SPANNABLE);
            txt_time.setText(DateUtils.getTimestampString(new Date(lastMessage.getMsgTime())));
            if (lastMessage.status == EMMessage.Status.SUCCESS) {
                txt_state.setText("送达");
            // txt_state.setBackgroundResource(R.drawable.btn_bg_orgen);
            } else if (lastMessage.status == EMMessage.Status.FAIL) {
                txt_state.setText("失败");
            // txt_state.setBackgroundResource(R.drawable.btn_bg_red);
            } else if (lastMessage.direct == EMMessage.Direct.RECEIVE) {
                txt_state.setText("已读");
                txt_state.setBackgroundResource(R.drawable.btn_bg_blue);
            }
        }
        txt_del.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                deleteID = position;
                Tipdialog = new WarnTipDialog((Activity) context, "您确定要删除该聊天吗?");
                Tipdialog.setBtnOkLinstener(onclick);
                Tipdialog.show();
            }
        });
    }
    return convertView;
}
Also used : EMConversation(com.easemob.chat.EMConversation) User(com.juns.wechat.bean.User) GroupInfo(com.juns.wechat.bean.GroupInfo) SwipeLayout(com.juns.wechat.widght.swipe.SwipeLayout) WarnTipDialog(com.juns.wechat.dialog.WarnTipDialog) OnClickListener(android.view.View.OnClickListener) TextView(android.widget.TextView) ImageView(android.widget.ImageView) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) Date(java.util.Date) EMMessage(com.easemob.chat.EMMessage)

Example 8 with EMMessage

use of com.easemob.chat.EMMessage in project wechat by motianhuo.

the class ChatActivity method sendFile.

/**
	 * 发送文件
	 * 
	 * @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.refresh();
    listView.setSelection(listView.getCount() - 1);
    setResult(RESULT_OK);
}
Also used : NormalFileMessageBody(com.easemob.chat.NormalFileMessageBody) Cursor(android.database.Cursor) File(java.io.File) EaseMobException(com.easemob.exceptions.EaseMobException) IOException(java.io.IOException) EMMessage(com.easemob.chat.EMMessage)

Example 9 with EMMessage

use of com.easemob.chat.EMMessage in project wechat by motianhuo.

the class ChatActivity method sendVideo.

/**
	 * 发送视频消息
	 */
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);
        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();
    }
}
Also used : File(java.io.File) EaseMobException(com.easemob.exceptions.EaseMobException) IOException(java.io.IOException) EMMessage(com.easemob.chat.EMMessage) VideoMessageBody(com.easemob.chat.VideoMessageBody)

Example 10 with EMMessage

use of com.easemob.chat.EMMessage in project wechat by motianhuo.

the class ChatActivity method sendLocationMsg.

/**
	 * 发送位置信息
	 * 
	 * @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);
    conversation.addMessage(message);
    listView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    listView.setSelection(listView.getCount() - 1);
    setResult(RESULT_OK);
}
Also used : EMMessage(com.easemob.chat.EMMessage) LocationMessageBody(com.easemob.chat.LocationMessageBody)

Aggregations

EMMessage (com.easemob.chat.EMMessage)13 File (java.io.File)6 EaseMobException (com.easemob.exceptions.EaseMobException)5 IOException (java.io.IOException)4 TextMessageBody (com.easemob.chat.TextMessageBody)3 Intent (android.content.Intent)2 View (android.view.View)2 OnClickListener (android.view.View.OnClickListener)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 ChatType (com.easemob.chat.EMMessage.ChatType)2 ImageMessageBody (com.easemob.chat.ImageMessageBody)2 Date (java.util.Date)2 SuppressLint (android.annotation.SuppressLint)1 Cursor (android.database.Cursor)1 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)1 OnLongClickListener (android.view.View.OnLongClickListener)1 LinearLayout (android.widget.LinearLayout)1 ProgressBar (android.widget.ProgressBar)1