参考:
基于环信即时通信云V2 SDK开发的聊天demo
Android基于环信SDK开发IM即时聊天(一)
android-使用环信SDK开发即时通信功能及源码下载
IM :
即时通信系统(IM,Instant Messenger)。
Android第三方即时聊天SDK的有:网易云信、环信和融云等。
参考:3款IM云服务产品对比 即时通讯云产品横向评测
这里以环信来做即时通讯demo达到两个效果:
1、用户登录,
2、用户间即时通讯
参考:Android开发课程之环信-即时通讯框架
环信的功能类似服务器。A和B用户的交互都要经过环信这个服务器。
环信框架集成
环信即时通讯云V2.0
环信即时通讯云V3.0
1、下载环信sdk开发包
环信sdk下载
下载后sdk目录如下:
2、注册成为环信开发者
开发者注册
3、在环信平台上创建应用程序,获取AppKey
4、在环信平台上注册用户
5、创建Android studio项目,导入SDK开发包中的库文件
5.1 将sdk包lib中的jar文件放到as项目中的libs中,并add library(可看源码即可)5.2 as项目src/main中new 一个directory,命名为jniLibs,然后将sdk包lib中的其他三个文件夹放到jniLibs中。
6、添加权限
7、配置AppKey
8、声明SDK所需的Service和BroadcastReceiver
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package="com.example.administrator.myapplication"><!-- Required --><uses-permission android:name="android.permission.VIBRATE" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.RECORD_AUDIO" /><uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.GET_TASKS" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /><uses-permission android:name="android.permission.WAKE_LOCK" /><uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><application android:name=".MyApplication"android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".ChatActivity"></activity><!-- 设置环信应用的AppKey --><meta-data android:name="EASEMOB_APPKEY"android:value="1173170609178552#chatdemo" /><!-- 声明SDK所需的service SDK核心功能--><service android:name="com.hyphenate.chat.EMChatService"android:exported="true" /><service android:name="com.hyphenate.chat.EMJobService"android:exported="true"android:permission="android.permission.BIND_JOB_SERVICE" /><!-- 声明SDK所需的receiver --><receiver android:name="com.hyphenate.chat.EMMonitorReceiver"><intent-filter><action android:name="android.intent.action.PACKAGE_REMOVED" /><data android:scheme="package" /></intent-filter><!-- 可选filter --><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED" /><action android:name="android.intent.action.USER_PRESENT" /></intent-filter></receiver></application></manifest>
9、SDK初始化
package com.example.administrator.myapplication;import android.app.Application;import com.hyphenate.chat.EMClient;import com.hyphenate.chat.EMOptions;/*** Created by Administrator on /6/9.*/public class MyApplication extends Application {@Overridepublic void onCreate() {super.onCreate();//配置环信sdkEMOptions emOptions = new EMOptions();emOptions.setAutoLogin(false);//初始化环信sdkEMClient.getInstance().init(this, emOptions);}}
10、登录
界面:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"tools:context="com.example.administrator.myapplication.MainActivity"><ImageView android:layout_width="match_parent"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher" /><EditText android:id="@+id/et_name_login"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:gravity="center"android:hint="请输入用户名" /><EditText android:id="@+id/et_pw_login"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:gravity="center"android:hint="请输入密码"android:inputType="textPassword" /><Button android:id="@+id/btn_login_login"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:paddingLeft="40dp"android:paddingRight="40dp"android:text="登录" /></LinearLayout>
代码:
package com.example.administrator.myapplication;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import com.hyphenate.EMCallBack;import com.hyphenate.chat.EMClient;import butterknife.BindView;import butterknife.ButterKnife;public class MainActivity extends AppCompatActivity {@BindView(R.id.et_name_login)EditText etNameLogin;@BindView(R.id.et_pw_login)EditText etPwLogin;@BindView(R.id.btn_login_login)Button btnLoginLogin;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButterKnife.bind(this);btnLoginLogin.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//执行登录功能EMClient.getInstance().login(etNameLogin.getText().toString(),//登录名称etPwLogin.getText().toString(),//登录密码new EMCallBack() {//子线程执行,不能更新UI@Overridepublic void onSuccess() {//登录成功startActivity(new Intent(MainActivity.this, ChatActivity.class));finish();}@Overridepublic void onError(int i, String s) {//登录失败}@Overridepublic void onProgress(int i, String s) {//进度更新}});}});}}
11、发送消息,监听消息的获取
布局:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextView android:id="@+id/tv"android:text="0000"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@+id/et_name_chat"android:background="@color/colorAccent" /><Button android:id="@+id/btn_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:text="发送" /><EditText android:id="@+id/et_name_chat"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_above="@+id/et_content_chat"android:layout_toLeftOf="@+id/btn_send"android:hint="请输入要发送的人" /><EditText android:id="@+id/et_content_chat"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignBottom="@+id/btn_send"android:layout_toLeftOf="@+id/btn_send"android:hint="请输入要发送的内容" /></RelativeLayout>
代码:
package com.example.administrator.myapplication;import android.os.Bundle;import android.os.Handler;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import com.hyphenate.EMMessageListener;import com.hyphenate.chat.EMClient;import com.hyphenate.chat.EMMessage;import com.hyphenate.chat.EMTextMessageBody;import java.util.List;import butterknife.BindView;import butterknife.ButterKnife;/*** Created by Administrator on /6/9.*/public class ChatActivity extends AppCompatActivity {@BindView(R.id.tv)TextView tv;@BindView(R.id.btn_send)Button btnSend;@BindView(R.id.et_content_chat)EditText etContentChat;@BindView(R.id.et_name_chat)EditText etNameChat;private MyEMMessageListener ml;private Handler handler = new Handler();@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main_chat);ButterKnife.bind(this);//发送消息btnSend.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//创建消息对象:参数:消息内容,接收人>>>此处注意顺序,容易出错导致接收不到信息 EMMessage txtSendMessage = EMMessage.createTxtSendMessage(etContentChat.getText().toString(),etNameChat.getText().toString());//发送消息EMClient.getInstance().chatManager().sendMessage(txtSendMessage);}});ml = new MyEMMessageListener();EMClient.getInstance().chatManager().addMessageListener(ml);}/** 消息监听*/class MyEMMessageListener implements EMMessageListener {@Overridepublic void onMessageReceived(List<EMMessage> list) {//此方法子线程中执行//解析消息for (EMMessage node : list) {final String from = node.getFrom();//发送者final EMMessage.Type type = node.getType();//消息类型switch (type) {case TXT://文本内容EMTextMessageBody body = (EMTextMessageBody) node.getBody();final String message = body.getMessage();//将修改内容交给主线程来更新UIhandler.post(new Runnable() {@Overridepublic void run() {tv.append(from + "对我说:" + message);}});break;default:break;}}}@Overridepublic void onCmdMessageReceived(List<EMMessage> list) {}@Overridepublic void onMessageRead(List<EMMessage> list) {}@Overridepublic void onMessageDelivered(List<EMMessage> list) {}@Overridepublic void onMessageChanged(EMMessage emMessage, Object o) {}}//取消监听避免内存泄漏@Overrideprotected void onDestroy() {super.onDestroy();EMClient.getInstance().chatManager().removeMessageListener(ml);}}