实例介绍
【实例简介】
【实例截图】
【核心代码】
package com.netease.nim.demo.chatroom.activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.netease.nim.demo.R;
import com.netease.nim.demo.chatroom.fragment.ChatRoomFragment;
import com.netease.nim.uikit.business.chatroom.fragment.ChatRoomMessageFragment;
import com.netease.nim.uikit.common.activity.UI;
import com.netease.nim.uikit.common.ui.dialog.DialogMaker;
import com.netease.nim.uikit.common.util.log.LogUtil;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.AbortableFuture;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.ResponseCode;
import com.netease.nimlib.sdk.StatusCode;
import com.netease.nimlib.sdk.chatroom.ChatRoomService;
import com.netease.nimlib.sdk.chatroom.ChatRoomServiceObserver;
import com.netease.nimlib.sdk.chatroom.model.ChatRoomInfo;
import com.netease.nimlib.sdk.chatroom.model.ChatRoomKickOutEvent;
import com.netease.nimlib.sdk.chatroom.model.ChatRoomStatusChangeData;
import com.netease.nimlib.sdk.chatroom.model.EnterChatRoomData;
import com.netease.nimlib.sdk.chatroom.model.EnterChatRoomResultData;
/**
* 聊天室
* Created by hzxuwen on 2015/12/14.
*/
public class ChatRoomActivity extends UI {
private final static String EXTRA_ROOM_ID = "ROOM_ID";
private static final String TAG = ChatRoomActivity.class.getSimpleName();
/**
* 聊天室基本信息
*/
private String roomId;
private ChatRoomInfo roomInfo;
private boolean hasEnterSuccess = false; // 是否已经成功登录聊天室
private ChatRoomFragment fragment;
/**
* 子页面
*/
private ChatRoomMessageFragment messageFragment;
private AbortableFuture<EnterChatRoomResultData> enterRequest;
public static void start(Context context, String roomId) {
Intent intent = new Intent();
intent.setClass(context, ChatRoomActivity.class);
intent.putExtra(EXTRA_ROOM_ID, roomId);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_room_activity);
roomId = getIntent().getStringExtra(EXTRA_ROOM_ID);
// 注册监听
registerObservers(true);
// 登录聊天室
enterRoom();
}
@Override
protected void onDestroy() {
super.onDestroy();
registerObservers(false);
}
@Override
public void onBackPressed() {
if (messageFragment == null || !messageFragment.onBackPressed()) {
super.onBackPressed();
}
logoutChatRoom();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (messageFragment != null) {
messageFragment.onActivityResult(requestCode, resultCode, data);
}
}
private void enterRoom() {
DialogMaker.showProgressDialog(this, null, "", true, new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (enterRequest != null) {
enterRequest.abort();
onLoginDone();
finish();
}
}
}).setCanceledOnTouchOutside(false);
hasEnterSuccess = false;
EnterChatRoomData data = new EnterChatRoomData(roomId);
enterRequest = NIMClient.getService(ChatRoomService.class).enterChatRoomEx(data, 1);
enterRequest.setCallback(new RequestCallback<EnterChatRoomResultData>() {
@Override
public void onSuccess(EnterChatRoomResultData result) {
onLoginDone();
roomInfo = result.getRoomInfo();
NimUIKit.enterChatRoomSuccess(result, false);
initChatRoomFragment();
initMessageFragment();
hasEnterSuccess = true;
}
@Override
public void onFailed(int code) {
onLoginDone();
if (code == ResponseCode.RES_CHATROOM_BLACKLIST) {
Toast.makeText(ChatRoomActivity.this, "你已被拉入黑名单,不能再进入", Toast.LENGTH_SHORT).show();
} else if (code == ResponseCode.RES_ENONEXIST) {
Toast.makeText(ChatRoomActivity.this, "聊天室不存在", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ChatRoomActivity.this, "enter chat room failed, code=" code, Toast.LENGTH_SHORT).show();
}
finish();
}
@Override
public void onException(Throwable exception) {
onLoginDone();
Toast.makeText(ChatRoomActivity.this, "enter chat room exception, e=" exception.getMessage(), Toast.LENGTH_SHORT).show();
finish();
}
});
}
private void registerObservers(boolean register) {
NIMClient.getService(ChatRoomServiceObserver.class).observeOnlineStatus(onlineStatus, register);
NIMClient.getService(ChatRoomServiceObserver.class).observeKickOutEvent(kickOutObserver, register);
}
private void logoutChatRoom() {
NIMClient.getService(ChatRoomService.class).exitChatRoom(roomId);
onExitedChatRoom();
}
public void onExitedChatRoom() {
NimUIKit.exitedChatRoom(roomId);
finish();
}
Observer<ChatRoomStatusChangeData> onlineStatus = new Observer<ChatRoomStatusChangeData>() {
@Override
public void onEvent(ChatRoomStatusChangeData chatRoomStatusChangeData) {
if (!chatRoomStatusChangeData.roomId.equals(roomId)) {
return;
}
if (chatRoomStatusChangeData.status == StatusCode.CONNECTING) {
DialogMaker.updateLoadingMessage("连接中...");
} else if (chatRoomStatusChangeData.status == StatusCode.LOGINING) {
DialogMaker.updateLoadingMessage("登录中...");
} else if (chatRoomStatusChangeData.status == StatusCode.LOGINED) {
if (fragment != null) {
fragment.updateOnlineStatus(true);
}
} else if (chatRoomStatusChangeData.status == StatusCode.UNLOGIN) {
if (fragment != null) {
fragment.updateOnlineStatus(false);
}
// 登录成功后,断网重连交给云信SDK,如果重连失败,可以查询具体失败的原因
if (hasEnterSuccess) {
int code = NIMClient.getService(ChatRoomService.class).getEnterErrorCode(roomId);
Toast.makeText(ChatRoomActivity.this, "getEnterErrorCode=" code, Toast.LENGTH_LONG).show();
LogUtil.d(TAG, "chat room enter error code:" code);
}
} else if (chatRoomStatusChangeData.status == StatusCode.NET_BROKEN) {
if (fragment != null) {
fragment.updateOnlineStatus(false);
}
Toast.makeText(ChatRoomActivity.this, R.string.net_broken, Toast.LENGTH_SHORT).show();
}
LogUtil.i(TAG, "chat room online status changed to " chatRoomStatusChangeData.status.name());
}
};
Observer<ChatRoomKickOutEvent> kickOutObserver = new Observer<ChatRoomKickOutEvent>() {
@Override
public void onEvent(ChatRoomKickOutEvent chatRoomKickOutEvent) {
Toast.makeText(ChatRoomActivity.this, "被踢出聊天室,原因:" chatRoomKickOutEvent.getReason(), Toast.LENGTH_SHORT).show();
onExitedChatRoom();
}
};
private void initChatRoomFragment() {
fragment = (ChatRoomFragment) getSupportFragmentManager().findFragmentById(R.id.chat_rooms_fragment);
if (fragment != null) {
fragment.updateView();
} else {
// 如果Fragment还未Create完成,延迟初始化
getHandler().postDelayed(new Runnable() {
@Override
public void run() {
initChatRoomFragment();
}
}, 50);
}
}
private void initMessageFragment() {
messageFragment = (ChatRoomMessageFragment) getSupportFragmentManager().findFragmentById(R.id.chat_room_message_fragment);
if (messageFragment != null) {
messageFragment.init(roomId);
} else {
// 如果Fragment还未Create完成,延迟初始化
getHandler().postDelayed(new Runnable() {
@Override
public void run() {
initMessageFragment();
}
}, 50);
}
}
private void onLoginDone() {
enterRequest = null;
DialogMaker.dismissProgressDialog();
}
public ChatRoomInfo getRoomInfo() {
return roomInfo;
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论