应用加入及时通讯,自己写,需要需要很多资源,继承第三方是面前比较快捷的办法,第三方有融云,环信,网易云信
最近集成融云的sdk,
安装官方说明引入jar包
RongIM.class 为融云消息启动类
RongCloudEvent.class为消息推送类
如果需要消息推送,这两个都要初始化:
在Application类中初始化
/**
* 初始化融云sdk
*/
private void initRongyunSdk() {
/**
* 注意:
*
* IMKit SDK调用第一步 初始化
*
* context上下文
*
* 只有两个进程需要初始化,主进程和 push 进程
*/
if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext())) ||
"io.rong.push".equals(getCurProcessName(getApplicationContext()))) {
RongIM.init(this);//主进程初始化
/**
* 融云SDK事件监听处理
*
* 注册相关代码,只需要在主进程里做。
*/
if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext()))) {
RongCloudEvent.init(this); //消息推送初始化
DemoContext.init(this);
Thread.setDefaultUncaughtExceptionHandler(new RongExceptionHandler(this));
try {
RongIM.registerMessageType(AgreedFriendRequestMessage.class);
RongIM.registerMessageTemplate(new ContactNotificationMessageProvider());
RongIM.registerMessageTemplate(new RealTimeLocationMessageProvider());
//@ 消息模板展示
RongContext.getInstance().registerConversationTemplate(new NewDiscussionConversationProvider());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
启动群聊:
if ( RongIM.getInstance()!=null) {
//发起聊天
RongIM.getInstance().startGroupChat(context, groupId, "群聊天");
// 单聊 RongIM.getInstance().startConversation(context, Conversation.ConversationType.PRIVATE, "9527", "聊天标题");
// 群聊 RongIM.getInstance().startPrivateChat(context, useridd, "聊天标题");
}else{
ToastUtils.show(context,"rongim 为空");
}
群聊天界面按说明文档,建立ConversationActivity并配置即可
群聊效果图:
可以动态集成也可以静态集成
静态集成
放到conversationlistactivity中静态集成 ,放到acticity中要在manifest文件中配置
设置为非聚合显示,否则要设置聚合显示的activity
public class ConversationListActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conversation_list);
enterFragment();
}
/**
* 加载 会话列表 ConversationListFragment
*/
private void enterFragment() {
ConversationListFragment fragment = (ConversationListFragment) getSupportFragmentManager().findFragmentById(R.id.list_conversation);
Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon()
.appendPath("conversationlist")
.appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话非聚合显示
.appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")//设置群组会话非聚合显示
.appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")//设置讨论组会话非聚合显示
.appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")//设置系统会话非聚合显示
.build();
fragment.setUri(uri);
}
}
静态集成:
不需要在manifest中配置,参考会融云话列表及界面实现的视频讲解:
http://www.rongcloud.cn/docs/android_video_tutorials.html
动态获取会话列表fragment
/**
* 会话列表的fragment
*/
private Fragment mConversationFragment = null;
/**
* 初始化会话列表
* @return 会话列表
*/
private Fragment initConversationList(){
if (mConversationFragment == null) {
ConversationListFragment listFragment = ConversationListFragment.getInstance();
Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon()
.appendPath("conversationlist")
.appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话是否聚合显示
.appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")//群组
.appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")//讨论组
.appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")//公共服务号
.appendQueryParameter(Conversation.ConversationType.APP_PUBLIC_SERVICE.getName(), "false")//订阅号
.appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")//系统
.build();
listFragment.setUri(uri);
return listFragment;
} else {
return mConversationFragment;
}
}
程序中要加入融云的消息推送类:
public final class RongCloudEvent implements RongIMClient.OnReceiveMessageListener, RongIM.OnSendMessageListener,
RongIM.UserInfoProvider, RongIM.GroupInfoProvider, RongIM.ConversationBehaviorListener,
RongIMClient.ConnectionStatusListener, RongIM.LocationProvider, RongIMClient.OnReceivePushMessageListener, RongIM.ConversationListBehaviorListener,
ApiCallback, Handler.Callback, RongIM.GroupUserInfoProvider {
//实现代码
}
推送效果图:
在控制的类中实现setOnReceiveUnreadCountChangedListener监听:
/**
* 融云消息接收,及初始化
*/
private void initRongMessage() {
final Conversation.ConversationType[] conversationTypes = {Conversation.ConversationType.PRIVATE, Conversation.ConversationType.DISCUSSION,
Conversation.ConversationType.GROUP, Conversation.ConversationType.SYSTEM,
Conversation.ConversationType.PUBLIC_SERVICE, Conversation.ConversationType.APP_PUBLIC_SERVICE};
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
RongIM.getInstance().setOnReceiveUnreadCountChangedListener(mCountListener, conversationTypes);
// RongIM.getInstance().setOnReceiveUnreadCountChangedListener(mCountListener1, Conversation.ConversationType.APP_PUBLIC_SERVICE);
}
}, 500);
}
控制消息的显示,消息数据的变化和隐藏
public RongIM.OnReceiveUnreadCountChangedListener mCountListener = new RongIM.OnReceiveUnreadCountChangedListener() {
@Override
public void onMessageIncreased(int count) {
if (count == 0) {
mUnreadNumView.setVisibility(View.GONE);
} else if (count > 0 && count < 100) {
mUnreadNumView.setVisibility(View.VISIBLE);
mUnreadNumView.setText(count + "");
} else {
mUnreadNumView.setVisibility(View.VISIBLE);
mUnreadNumView.setText(R.string.no_read_message);
}
}
};
显示消息效果图:
没有消息:
收到一条消息:
消息数据变化:
/**
* 用户信息的提供者:GetUserInfoProvider 的回调方法,获取用户信息。
*
* @param userId 用户 Id。
* @return 用户信息,(注:由开发者提供用户信息)。
*/
@Override
public UserInfo getUserInfo(String userId) {
if (userId == null)
return null;
if (DemoContext.getInstance() == null)
return null;
UserInfos userInfo = DemoContext.getInstance().getUserInfosById(userId);
if (userInfo == null) {
getUserInfoByUserIdHttpRequest = DemoContext.getInstance().getDemoApi().getUserInfoByUserId(userId, (ApiCallback) this);
}
final UserInfo userInfoById = DemoContext.getInstance().getUserInfoById(userId);
return userInfoById;
/* String url="http://img01.taopic.com/141012/235112-1410120K20374.jpg";
// final String uriString = "http://img1.2345.com/duoteimg/qqTxImg/2013/04/22/13667759472.jpg";
String uri = "http://dev1.mobile.xiebao.me/attachment/download?username=888880&sid=0ah1i8he53pfe8fie5bh6f09h5&file_id=7845&assoc_type=agreelogo1&type=short";
UserInfo user= new UserInfo(userId, "xiaowang", Uri.parse(uri));
Log.v(tag,"设置用户头像,id = "+userId);
return user;*/
}
效果图: