现阶段来看,直播软件中的即时通讯是非常重要的一个部分,毕竟直播过程中的交流和沟通是非常重要的,所以在Android直播软件开发时需要接入相关的IM服务。
通常我们选择的即时聊天服务,会选择集成简单方便、系统稳定、可靠性好、开发成本小的一方,目前比较常见的IM服务商有极光、环信、腾讯、融云等等,本文以选择腾讯IM为例,简单说下如何在Android直播软件开发时集成腾讯IM。
先登录腾讯云官网 https://cloud.tencent.com/,在产品中搜索即时通信IM。
登录进入控制台,并创建应用
创建应用成功,按照文档说明进行相应的配置
样就完成应用的创建和配置。
编写android代码,集成IM
@Override
public void init() {
TIMManager timManager = TIMManager.getInstance();
//判断是否是在主线程 初始化 SDK 基本配置
if (SessionWrapper.isMainProcess(CommonAppContext.sInstance)) {
TIMSdkConfig config = new TIMSdkConfig(CommonAppConfig.TX_IM_APP_Id)
.enableLogPrint(false)
.setLogLevel(TIMLogLevel.OFF);
timManager.init(CommonAppContext.sInstance, config);
}
//基本用户配置
TIMUserConfig userConfig = new TIMUserConfig();
//设置用户状态变更事件监听器
userConfig.setUserStatusListener(new TIMUserStatusListener() {
@Override
public void onForceOffline() {
L.e(TAG, "被其他终端顶掉了---->");
RouteUtil.forwardLoginInvalid(WordUtil.getString(R.string.login_status_Invalid));
}
@Override
public void onUserSigExpired() {
L.e(TAG, "用户签名过期了,需要重新登录---->");
}
});
//设置连接状态事件监听器
userConfig.setConnectionListener(new TIMConnListener() {
@Override
public void onConnected() {
L.e(TAG, "连接成功---->");
if (mHandler != null) {
mHandler.removeCallbacksAndMessages(null);
}
}
@Override
public void onDisconnected(int code, String desc) {
L.e(TAG, "连接断开---->");
if (mHandler != null) {
mHandler.sendEmptyMessageDelayed(0, 2000);
}
}
@Override
public void onWifiNeedAuth(String name) {
L.e(TAG, "onWifiNeedAuth");
}
});
//设置会话刷新监听器
userConfig.setRefreshListener(new TIMRefreshListener() {
@Override
public void onRefresh() {
//L.e(TAG, "刷新会话---onRefresh----->");
}
@Override
public void onRefreshConversation(List conversations) {
// L.e(TAG, "刷新会话---onRefreshConversation---->size: " + conversations.size());
}
});
timManager.setUserConfig(userConfig);
timManager.addMessageListener(TxImMessageUtil.this);
// 设置离线推送监听器
timManager.setOfflinePushListener(this);
}
@Override
public void loginImClient(String uid) {
String sign = SpUtil.getInstance().getStringValue(SpUtil.TX_IM_USER_SIGN);
if (TextUtils.isEmpty(sign)) {
ToastUtil.show("腾讯IM登录失败! 签名错误!");
return;
}
TIMManager.getInstance().login(uid, sign, new TIMCallBack() {
@Override
public void onError(int code, String desc) {
L.e(TAG, "登录失败 : " + code + " errmsg: " + desc);
CommonAppConfig.getInstance().setLoginIM(false);
ToastUtil.show("IM 登录失败:" + code + " errmsg: " + desc);
}
@Override
public void onSuccess() {
L.e(TAG, "登录成功!!");
CommonAppConfig.getInstance().setLoginIM(true);
refreshAllUnReadMsgCount();
ConfigBean configBean = CommonAppConfig.getInstance().getConfig();
if (configBean != null) {
String groupId = configBean.getTxImGroupId();
L.e(TAG, "群组ID------> " + groupId);
if (!TextUtils.isEmpty(groupId)) {
mGroupId = groupId;
TIMGroupManager.getInstance().applyJoinGroup(groupId, "login", new TIMCallBack() {
@java.lang.Override
public void onError(int code, String desc) {
L.e(TAG, "加入群组失败 : " + code + " errmsg: " + desc);
}
@java.lang.Override
public void onSuccess() {
L.e(TAG, "加入群组成功!!");
}
});
}
}
}
});
}
@Override
public void logoutImClient() {
TIMManager timManager = TIMManager.getInstance();
timManager.logout(null);
CommonAppConfig.getInstance().setLoginIM(false);
L.e(TAG, "退出登录--->");
}
/**
* 设置消息监听器,收到新消息时,通过此监听器回调
*
* @param list
* @return
*/
@Override
public boolean onNewMessages(List list) {
if (list == null || list.size() == 0) {
return true;
}
for (TIMMessage msg : list) {
onReceiveMessage(msg);
}
return true; //返回true将终止回调链,不再调用下一个新消息监听器
}
/**
* 标记所有会话为已读 即忽略所有未读
*/
@Override
public void markAllConversationAsRead() {
List list = TIMManagerExt.getInstance().getConversationList();
if (list != null && list.size() > 0) {
for (TIMConversation conversation : list) {
if (conversation != null) {
TIMConversationExt conExt = new TIMConversationExt(conversation);
conExt.setReadMessage(null, mEmptyCallBack);
}
}
}
EventBus.getDefault().post(new ImUnReadCountEvent("0"));
}
@Override
public ImMessageBean createTextMessage(String toUid, String content) {
TIMMessage msg = new TIMMessage();
TIMTextElem elem = new TIMTextElem();
elem.setText(content);
if (msg.addElement(elem) != 0) {
L.e(TAG, "发送文本消息失败----->");
return null;
}
return new ImMessageBean(CommonAppConfig.getInstance().getUid(), msg, ImMessageBean.TYPE_TEXT, true);
}
@Override
public ImMessageBean createImageMessage(String toUid, String path) {
TIMMessage msg = new TIMMessage();
TIMImageElem elem = new TIMImageElem();
elem.setPath(path);
if (msg.addElement(elem) != 0) {
L.e(TAG, "发送图片消息失败----->");
return null;
}
return new ImMessageBean(CommonAppConfig.getInstance().getUid(), msg, ImMessageBean.TYPE_IMAGE, true);
}
@Override
public ImMessageBean createLocationMessage(String toUid, double lat, double lng, int scale, String address) {
TIMMessage msg = new TIMMessage();
TIMLocationElem elem = new TIMLocationElem();
elem.setLatitude(lat);
elem.setLongitude(lng);
elem.setDesc(address);
if (msg.addElement(elem) != 0) {
return null;
}
return new ImMessageBean(CommonAppConfig.getInstance().getUid(), msg, ImMessageBean.TYPE_LOCATION, true);
}
@Override
public ImMessageBean createVoiceMessage(String toUid, File voiceFile, long duration) {
TIMMessage msg = new TIMMessage();
TIMSoundElem elem = new TIMSoundElem();
elem.setPath(voiceFile.getAbsolutePath());
elem.setDuration(duration / 1000);
if (msg.addElement(elem) != 0) {
return null;
}
return new ImMessageBean(CommonAppConfig.getInstance().getUid(), msg, ImMessageBean.TYPE_VOICE, true);
}
/**
* 发送自定义消息
*
* @param toUid
* @param data 要发送的数据
*/
@Override
public void sendCustomMessage(String toUid, String data, final boolean save) {
final TIMMessage customMsg = new TIMMessage();
TIMCustomElem elem = new TIMCustomElem();
elem.setData(data.getBytes());
if (customMsg.addElement(elem) != 0) {
return;
}
TIMConversation conversation = TIMManager.getInstance().getConversation(TIMConversationType.C2C, getImUid(toUid));
if (conversation != null) {
if (mSendCustomCallback == null) {
mSendCustomCallback = new TIMValueCallBack() {//发送消息回调
@Override
public void onError(int code, String desc) {//发送消息失败
L.e(TAG, "发送自定义消息失败---> code: " + code + " errmsg: " + desc);
if (!save) {
removeMessage(customMsg);
}
}
@Override
public void onSuccess(TIMMessage msg) {//发送消息成功
L.e(TAG, "发送自定义消息成功!!");
if (!save) {
removeMessage(customMsg);
}
}
};
}
conversation.sendMessage(customMsg, mSendCustomCallback);
}
}
@Override
public void sendMessage(String toUid, ImMessageBean bean, SendMsgResultCallback callback) {
if (bean == null || TextUtils.isEmpty(toUid)) {
return;
}
TIMMessage msg = bean.getTimRawMessage();
if (msg == null) {
return;
}
TIMConversation conversation = TIMManager.getInstance().getConversation(TIMConversationType.C2C, getImUid(toUid));
if (conversation != null) {
mSendMsgResultCallback = callback;
if (mSendCompleteCallback == null) {
mSendCompleteCallback = new TIMValueCallBack() {//发送消息回调
@Override
public void onError(int code, String desc) {//发送消息失败
L.e(TAG, "发送消息失败---> code: " + code + " errmsg: " + desc);
if (mSendMsgResultCallback != null) {
mSendMsgResultCallback.onSendFinish(false);
}
mSendMsgResultCallback = null;
}
@Override
public void onSuccess(TIMMessage msg) {//发送消息成功
if (mSendMsgResultCallback != null) {
mSendMsgResultCallback.onSendFinish(true);
}
mSendMsgResultCallback = null;
}
};
}
conversation.sendMessage(msg, mSendCompleteCallback);
}
}
/**
* 删除消息
*/
private boolean removeMessage(TIMMessage msg) {
if (msg != null) {
TIMMessageExt timMessageExt = new TIMMessageExt(msg);
return timMessageExt.remove();
}
return false;
}
这样就实现了私信聊天功能。
以上就是在Android直播软件开发时集成腾讯IM的流程,感兴趣可持续关注。
声明:本文由作者原创,转载须备注文章来源与作者。