环信的集成
1.注册
1.打开首页
https://www.easemob.com/
2.点击注册,选择注册即时通信云
3.输入注册信息
4.选择个人开发者
5.去邮箱激活账号
6.登录
7.登录后,有一个默认的app
8.在这个默认的app中新建两个user
2、集成
1.下载demo
2.新建工程
3.将easy导入,注意,android studio最新版默认自持androidx
4.新工程的app module需要依赖easyui
5.
<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" />
6.新工程的manifes中需要添加:
<meta-data
android:name="EASEMOB_APPKEY"
android:value="1106201125030909#demo" /> <!-- 声明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>
注意app key需要修改
7.新建App类,继承于Application
8.App中做初始化
EMOptions options = new EMOptions();
//EaseUI初始化
if (EaseUI.getInstance().init(getApplicationContext(), options)) {
//在做打包混淆时,关闭debug模式,避免消耗不必要的资源
EMClient.getInstance().setDebugMode(true);
//EaseUI初始化成功之后再去调用注册消息监听的代码
}
9.权限
10.
Cleartext HTTP traffic,可以讲target sdk 改为26
android:requestLegacyExternalStorage="true"
android:usesCleartextTraffic="true"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
11、登录
private void login() {
EMClient.getInstance().login("u1", "123", new EMCallBack(){
@Override
public void onSuccess() {
Log.i("Simon", "login onSuccess");
}
@Override
public void onError(int code, String error) {
Log.i("Simon", "login onError: " + code + " msg: " + error);
}
@Override
public void onProgress(int progress, String status) {
}
});
}
12、登出
EMClient.getInstance().logout(true, new EMCallBack() {
@Override
public void onSuccess() {
Log.i("Simon", "logout onSuccess");
}
@Override
public void onError(int code, String error) {
Log.i("Simon", "logout onError: " + code + " msg: " + error);
}
@Override
public void onProgress(int progress, String status) {
}
});
13、打开会话页面
Intent intent = new Intent(MainActivity.this, ChatActivity.class);
//username为对方的环信id
intent.putExtra(EaseConstant.EXTRA_USER_ID, "u2");
startActivity(intent);
public class ChatActivity extends AppCompatActivity {
EaseChatFragment chatFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
//use EaseChatFratFragment
chatFragment = new EaseChatFragment();
//pass parameters to chat fragment
chatFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
}
}
14、
public class ConversationListFragment extends EaseConversationListFragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
conversationListView = view.findViewById(R.id.list);
conversationListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
EMConversation conversation = conversationListView.getItem(position);
String username = conversation.conversationId();
if (username.equals(EMClient.getInstance().getCurrentUser()))
Toast.makeText(getActivity(), R.string.Cant_chat_with_yourself, Toast.LENGTH_SHORT).show();
else {
// start chat acitivity
Intent intent = new Intent(getActivity(), ChatActivity.class);
if(conversation.isGroup()){
if(conversation.getType() == EMConversation.EMConversationType.ChatRoom){
// it's group chat
intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_CHATROOM);
}else{
intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_GROUP);
}
}
// it's single chat
intent.putExtra(EaseConstant.EXTRA_USER_ID, username);
startActivity(intent);
}
}
});
}
}
public class ConversationListActivity extends AppCompatActivity {
ConversationListFragment mConversationListFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversation_list);
//use EaseChatFratFragment
mConversationListFragment = new ConversationListFragment();
getSupportFragmentManager().beginTransaction().add(R.id.container, mConversationListFragment).commit();
}
}