融云会话界面提示聊天服务器断开
经过调试发现,app关闭屏幕后,手机系统把rongIMClient回收了
于是采用反射方式,每次进入应用的时候,判断rongIMClient是否为空,为空的时候表示被系统回收,采用反射方式初始化rongIMClient。
/**
* 反射方式,检测融云服务
*/
private void checkRongIMClientServiceStatus() {
try {
RongIMClient rongIMClient = RongIMClient.getInstance();
Class<?> classType = rongIMClient.getClass();
Field field = classType.getDeclaredField("mLibHandler");
field.setAccessible(true); // 抑制Java对修饰符的检查
if (field.get(rongIMClient) == null) {
java.lang.reflect.Method method = classType.getDeclaredMethod("initBindService");
method.setAccessible(true);// 使得这个方法允许被调用,不然会抛异常,私有方法不允许被调用的异常
method.invoke(rongIMClient);
toast("聊天服务器断开,正在重连...");
}
} catch (Exception e) {
e.printStackTrace();
}
}
这个方法后可以加
//如果检测到没有连接,应该重新发起连接请求
if (!RongIM.getInstance().getCurrentConnectionStatus().equals(RongIMClient.ConnectionStatusListener.ConnectionStatus.CONNECTED) || RongIM.getInstance().getCurrentConnectionStatus().equals(RongIMClient.ConnectionStatusListener.ConnectionStatus.CONNECTING)) {
RongIM.connect(Utils.getSpUtils().getString(SealConst.IQ_CLOUD_TOKEN), SealAppContext.getInstance().getConnectCallback());
}
这个方法建议放在页面的onStart()里面