基于环信即时通信云V2 SDK开发的聊天demo
Android基于环信SDK开发IM即时聊天(一)
android-使用环信SDK开发即时通信功能及源码下载
即时通信系统(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
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.myapplication">
<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>
<meta-data
android:name="EASEMOB_APPKEY"
android:value="1173170609178552#chatdemo" />
<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" />
<receiver android:name="com.hyphenate.chat.EMMonitorReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
intent-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 2017/6/9.
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//配置环信sdk
EMOptions emOptions = new EMOptions();
emOptions.setAutoLogin(false);
//初始化环信sdk
EMClient.getInstance().init(this, emOptions);
}
}
10、登录
界面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
btnLoginLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//执行登录功能
EMClient.getInstance().login(etNameLogin.getText().toString(),//登录名称
etPwLogin.getText().toString(),//登录密码
new EMCallBack() {//子线程执行,不能更新UI
@Override
public void onSuccess() {
//登录成功
startActivity(new Intent(MainActivity.this, ChatActivity.class));
finish();
}
@Override
public void onError(int i, String s) {
//登录失败
}
@Override
public void onProgress(int i, String s) {
//进度更新
}
});
}
});
}
}
11、发送消息,监听消息的获取
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/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 2017/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();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_chat);
ButterKnife.bind(this);
//发送消息
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public 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 {
@Override
public void onMessageReceived(List 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();
//将修改内容交给主线程来更新UI
handler.post(new Runnable() {
@Override
public void run() {
tv.append(from + "对我说:" + message);
}
});
break;
default:
break;
}
}
}
@Override
public void onCmdMessageReceived(List list) {
}
@Override
public void onMessageRead(List list) {
}
@Override
public void onMessageDelivered(List list) {
}
@Override
public void onMessageChanged(EMMessage emMessage, Object o) {
}
}
//取消监听避免内存泄漏
@Override
protected void onDestroy() {
super.onDestroy();
EMClient.getInstance().chatManager().removeMessageListener(ml);
}
}