环信SDK调⽤实现即时聊天功能
最近在做个项⽬,需要⽤到即时通信的功能,选定了环信的IM云来实现,关于环信IM的SDK接⼝,⾃⼰研究了⼀天,基本的功能实现了⼀下,这⾥PO出来demo。
因为只是为了验证sdk的实现,所以界⾯设计的⽐较丑,还请谅解。还有,只⽤到了⽂本消息,所以选取的sdk版本是2.X。
具体的下载链接见⽂末。
先上图
⾸先是⼀张消息列表,列表中的项⽬包括⽤户头像、⽤户名和最后⼀条消息显⽰。
第⼆张是会话,简单点来写,每⼀个项的list view的adapter都没有设置,只是简单的显⽰消息。点击button向对⽅发送“呵呵呵”的消息。
下⾯来阐述详细的实现。
step 1: 在manifest⽂件中添加users-permission 和appkey的service
这⾥的permission内容可以从环信⽂档中获取,⼤致如下
<!-- 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" />
另外的service需要的apply如下,这⾥选取的是环信提供的默认appkey
<!-- 设置环信应⽤的appkey -->
<meta-data android:name="EASEMOB_APPKEY" android:value="easemob-demo#chatdemoui" />
<!-- 声明sdk所需的service SDK核⼼功能-->
<service android:name="com.easemob.chat.EMChatService" />
然后将sdk⽂件复制到libs⽂件夹中即可。这样就完成了SDK的导⼊⼯作
step 2: 环信账号登录
在登录之前,要在主的application⽂件中加上⼀条语句:
保证app在⾸次打开时就初始化EMChat实例
然后,登录环信账号,这⾥需要⽤到当前的appkey注册到的账号和密码,我⾃⼰注册的测试⽤例的登录过程如下:
ample.demo;
import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import android.app.Activity;
t.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String UA = "blyang";
public static final String PA = "bing";
public static final String UB = "yangbingliang";
public static final String PB = "bing";
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
login(UA, PA);
}
private void login(String userName, String password){
@Override
public void onError(int arg0, String arg1) {
Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show();
}
@Override
public void onProgress(int arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable(){
@Override
public void run() {
Toast.makeText(MainActivity.this, "success", Toast.LENGTH_LONG).show(); }
});
//从本地DB加载到程序中
startActivity(new Intent(MainActivity.this, Container.class));
finish();
}
});
}
}
登录的过程就是调⽤EMChatManager来实现,登录成功后,进⾏相关的操作。这⾥在登录成功后,加⼊了⼀句:
//从本地DB加载到程序中
是为了保证从本地数据库中取出历史所有的conversation对象并加载出来
step 3: 将历史对话数据加载成消息列表的形式
⾸先创建⼀个conversationList 的List对象,然后将最近的所有会话添加到这个list中,最后,调⽤消息列表展⽰到adapter,将数据传⼊并展⽰出来。具体到代码和注释如下:
ample.demo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.List;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import android.app.Fragment;
t.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class ChatHistoryFragment extends Fragment{
//所有的会话列表
private List<EMConversation> conversationList = new ArrayList<EMConversation>();
ChatHistoryAdapter adapter ;
ListView chatHistoryListView;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_conversation_history, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
conversationList.addAll(loadConversationWithRecentChat());
chatHistoryListView = (ListView) view.findViewById(R.id.list);
adapter = new ChatHistoryAdapter(getActivity(), conversationList);
chatHistoryListView.setAdapter(adapter);
chatHistoryListView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
EMConversation conversation = (EMConversation) Item(position);sdk
Intent intent = new Intent(getActivity(), Conversation.class);
intent.putExtra("userName", UserName());
startActivity(intent);
}
});
int count = 0;
for(int i=0; i<conversationList.size(); i++){
count += (i).getUnreadMsgCount();
}
Log.v("count total", count+"");
}
/**
* 获取所有会话
* @return
*/
private Collection<? extends EMConversation> loadConversationWithRecentChat() {
Hashtable<String, EMConversation> conversations = EMChatManager
.getInstance().getAllConversations();
List<Pair<Long, EMConversation>> sortList = new ArrayList<Pair<Long, EMConversation>>(); synchronized(conversations){
for(EMConversation conversation : conversations.values()){
AllMessages().size() != 0){
sortList.add(new Pair<Long, EMConversation>
(LastMessage().getMsgTime(), conversation)
);
}
}
}
try{
sortConversationByLastChatTime(sortList);
}catch(Exception e){
e.printStackTrace();
}
List<EMConversation> list = new ArrayList<EMConversation>();
for(Pair<Long, EMConversation> sortItem : sortList){
list.add(sortItem.second);
}
return list;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论