环信发送名片

环信需要发送名片的时候,首先在需要发送的地方new处这个message
EMMessage message = EMMessage.createTxtSendMessage(name, user_id);
name为名片的content,后面的user_id为要发送的对方的用户名
message.setAttribute(HxOutMessage.myExtType,"mingpianType");
里面带的内容为需要发送的信息
单人的不需要设置,如果是群聊或者聊天室,需要设置
message.setChatType(EMMessage.ChatType.GroupChat);
发送消息
EMClient.getInstance().chatManager().sendMessage(message);
发送完消息,跳转到对方的聊天页面
    intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE,EaseConstant.CHATTYPE_GROUP);
intent.putExtra(EaseConstant.EXTRA_USER_ID, bean.getCrowd_id());
startActivity(intent);
发送完消息以后,需要接受,在EaseMessageAdapter这个类里面接受
新建两个int的标记
private static final int  MESSAGE_TYPE_RECV_MINGPIAN_CALL=14;
private static final int  MESSAGE_TYPE_SENT_MINGPIAN_CALL=15;
在getviewtypecount的方法里面
public int getViewTypeCount() {
    if(customRowProvider != null && customRowProvider.getCustomChatRowTypeCount() > 0){
        return customRowProvider.getCustomChatRowTypeCount() + 16;//原来为14,加两个
    }
       return 16;
   }
在getitemviewtype的方法里面添加
if (!TextUtils.isEmpty(message.getStringAttribute(HxOutMessage.myExtType,null))){
   return message.direct()== EMMessage.Direct.SEND ?MESSAGE_TYPE_SENT_MINGPIAN_CALL :MESSAGE_TYPE_RECV_MINGPIAN_CALL;
}

createChatRow的这个方法里面添加在switch方法里面选择TXT的选择里面
 case TXT:
        if(message.getBooleanAttribute(EaseConstant.MESSAGE_ATTR_IS_BIG_EXPRESSION, false)){
            chatRow = new EaseChatRowBigExpression(context, message, position, this);
        }else{
if (!TextUtils.isEmpty(message.getStringAttribute(HxOutMessage.myExtType,null))){
   chatRow=new MingPianChatRow(context,message,position,this);
	//MingPianChatRow为自己新建的EaseChatRow,自己想要发出的消息是什么样,自己去xml里面写页面

}else {
   chatRow = new EaseChatRowText(context, message, position, this);
	
}

        }
        break;

自己新建一个实体类,用来存放名片的信息 
public class HxOutMessage {
  public static final String myExtType="myExtType";
  public static final String id="19";
  public static final String name="小胡子";
  public static final String autho="https://www.baidu.com/img/bd_logo1.png";
  public static final String num="10";

}
最后是自己新建的MingPianChatRow
public class MingPianChatRow extends EaseChatRow {
    private ImageView autho_im;
    private TextView name_tx;
    private TextView id_tx;
    private String message_id;
    private String from;
    private TextView type_tx;
    private String name;
    private String id;
    private String content;
    private  String autho;
    private String active_id;
    private String shareid;

    public MingPianChatRow(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    @Override
    protected void onInflateView() {
    if (!TextUtils.isEmpty(message.getStringAttribute(HxOutMessage.myExtType,null))){
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_chat_mingpian_recive : R.layout.ease_chat_mingpian_send, this);
    }
    }

    @Override
    protected void onFindViewById() {
        autho_im = ((ImageView) findViewById(R.id.autho_person));
        name_tx = ((TextView) findViewById(R.id.tx_name_person));
        id_tx = ((TextView) findViewById(R.id.id_person));
        type_tx = ((TextView) findViewById(R.id.from_share));
    }

    @Override
    protected void onUpdateView() {

    }

    @Override
    protected void onSetUpView() {
	//接受message,显示名片的内容
        EMTextMessageBody body = (EMTextMessageBody) message.getBody();
   if (message.direct()==EMMessage.Direct.RECEIVE){
       name_tx.setText(body.getMessage());
   }
        if (message.getBooleanAttribute("mingpianType",true)){
            message.getStringAttribute(HxOutMessage.myExtType,null);
            message_id = message.getStringAttribute(HxOutMessage.id,null);
            name = message.getStringAttribute(HxOutMessage.name, null);
            autho = message.getStringAttribute(HxOutMessage.autho, null);
            content = message.getStringAttribute("content",null);
            from = message.getStringAttribute("from",null);
            id = message.getStringAttribute("goodid",null);
            active_id = message.getStringAttribute("active_id","0");
            shareid = message.getStringAttribute("shareid","0");
           
        }
    }

    @Override
    protected void onBubbleClick() {
       //名片的点击事件在这里写

    }
}
//
ease_chat_mingpian_recive这个xml页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:orientation="vertical"
              android:padding="13dp"
              android:layout_height="match_parent">
    <TextView
        android:id="@+id/timestamp"
        style="@style/chat_text_date_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin_chat_activity" >

        <ImageView
            android:id="@+id/iv_userhead"
            android:layout_width="@dimen/size_avatar"
            android:layout_height="@dimen/size_avatar"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="@dimen/margin_chat_activity"
            android:scaleType="fitXY" />

        <RelativeLayout
            android:id="@+id/bubble"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_userid"
            android:layout_marginLeft="@dimen/margin_chat_activity"
            android:layout_toRightOf="@id/iv_userhead"
            android:background="@drawable/ease_chatfrom_bg"
            >
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:id="@+id/autho_person"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_margin="10dp"
                    android:scaleType="centerCrop"
                    android:src="@mipmap/b"
                    />
                <TextView
                    android:id="@+id/tx_name_person"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_toRightOf="@id/autho_person"
                    android:text="name"
                    android:textColor="@color/text_color_b"
                    android:textSize="15sp"
                    />

                <TextView
                    android:id="@+id/id_person"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@id/autho_person"
                    android:layout_marginLeft="10dp"
                    android:layout_toRightOf="@id/autho_person"
                    android:text="id"
                    android:textColor="@color/text_color_b"
                    android:textSize="15sp"
                    />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:id="@+id/line"
                    android:layout_below="@id/autho_person"
                    android:background="@color/line_color"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/line"
                    android:textSize="12sp"
                    android:id="@+id/from_share"
                    android:textColor="@color/text_color_w"
                    android:layout_margin="10dp"
                    />
            </RelativeLayout>

        </RelativeLayout>

        <TextView
            android:id="@+id/tv_userid"
            style="@style/chat_text_name_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/chat_nick_margin_left"
            android:textSize="@dimen/chat_nick_text_size"
            android:layout_toRightOf="@id/iv_userhead"
            android:visibility="gone" />
    </RelativeLayout>
</LinearLayout>


完事,就这么写,哈哈


要在Android应用程序中集成环信的EaseIMKit来发送语音,需要遵循以下步骤: 1. 在项目的build.gradle文件中添加环信库的依赖: ``` dependencies { // 环信IM SDK implementation 'com.hyphenate:chat-sdk:3.5.2' // 环信UI库 implementation 'com.hyphenate:chat-ui-sdk:3.5.2' } ``` 2. 在AndroidManifest.xml文件中声明必要的权限: ``` <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.VIBRATE" /> ``` 3. 在你的Activity中创建EaseChatFragment实例,并将其添加到布局中: ``` <fragment android:id="@+id/chat_fragment" android:name="com.hyphenate.easeui.ui.EaseChatFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/top_bar" android:background="@color/white" app:layout_behavior="@string/appbar_scrolling_view_behavior" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:layout_anchor="@id/top_bar" app:layout_anchorGravity="bottom|center_horizontal" /> ``` 4. 在Activity中初始化EaseChatFragment,并设置相关属性: ``` private void initChatFragment() { // 获取EaseChatFragment实例 EaseChatFragment chatFragment = (EaseChatFragment) getSupportFragmentManager().findFragmentById(R.id.chat_fragment); // 设置聊天界面的标题 chatFragment.setTitle("Chat with xxx"); // 设置聊天对象的ID chatFragment.setArguments(getIntent().getExtras()); // 设置是否开启语音消息发送功能 chatFragment.setShowRecord(true); } ``` 5. 在EaseChatFragment中实现发送语音消息的逻辑: ``` // 在EaseChatFragment中重写onSendVoice方法 @Override public void onSendVoice(String filePath, int length) { // 创建语音消息 EMVoiceMessageBody messageBody = new EMVoiceMessageBody(new File(filePath), length); EMMessage message = EMMessage.createVoiceSendMessage(filePath, length, toChatUsername); // 发送语音消息 sendMessage(message); } ``` 以上就是使用环信的EaseIMKit实现Android发送语音消息的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值