前言
1、融云IM应该说是目前最好自定义和易读取文档的即时通讯第三方SDK了,之前有用过阿里百川IM的,可惜阿里百川不再更新和维护了
2、im千万别选QQ的,如果就普通聊天选择qq那没问题,如果业务需要自定义的果断放弃。融云IM一直在快速的更新和维护。选它就没错了
步骤1:创建应用,导入SDK
创建应用:记得开发环境和生产环境是分开的,开发环境可生成的IM账号是有限的,有时im登录不成功是因为没账号.
导入SDK:强烈推荐以导入 Module 方式引入IMKit,IMLib。方便修改
步骤2:初始化
public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        RongIM.init(this);
    }
}
步骤3:登录IM
从app服务器端获取token,然后调用RongIM.connect(token, new RongIMClient.ConnectCallback())即可
一般调用时机是在首页,看业务也可以在启动界面。
注意
- 调用成功一次就好
 - 服务器获取的token,可以用SP保存下来,因为是长期有效的,不用每次都去获取
 
示例代码:
public static void connect() {
    if (Utils.isNetworkConnected(MyApplication.getInstance()))
        if (!TextUtils.isEmpty(SPUtils.getToken())) {
            connect(SPUtils.getToken(),null);
        } else {
            //正常只有第一次进入应用会为""
            new BaseIMPresenter().getToken(false, new EmptyTokenListener() {
                @Override
                public void getToken(String token) {
                    connect(token, null);
                }
            });
        }
}
/**
 * 连接服务器,在整个应用程序全局,只需要调用一次,需在 {@link #init(Context)} 之后调用。
 * 如果调用此接口遇到连接失败,SDK 会自动启动重连机制进行最多10次重连,分别是1, 2, 4, 8, 16, 32, 64, 128, 256, 512秒后。
 * 在这之后如果仍没有连接成功,还会在当检测到设备网络状态变化时再次进行重连。
 *
 * @param token 从服务端获取的用户身份令牌(Token)。
 * @return RongIM  客户端核心类的实例。
 */
private static void connect(String token, final ConnectListener cl) {
    if (BuildConfig.APPLICATION_ID.equals(MyApplication.getCurProcessName(MyApplication.getInstance()))) {
        RongIM.connect(token, new RongIMClient.ConnectCallback() {
            /**
             * Token 错误。可以从下面两点检查
             * 1.  Token 是否过期,如果过期您需要向 App Server 重新请求一个新的 Token
             * 2.  token 对应的 appKey 和工程里设置的 appKey 是否一致
             */
            @Override
            public void onTokenIncorrect() {
                if (null != cl)
                    cl.onTokenIncorrect();
                new BaseIMPresenter().getToken(true, null);
            }
            /**
             * 连接融云成功
             * @param userid 当前 token 对应的用户 id
             */
            @Override
            public void onSuccess(String userid) {
            }
            /**
             * 连接融云失败
             * @param errorCode 错误码,可到官网 查看错误码对应的注释
             */
            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {
            }
        });
    }
}
额外提供IM是否在线的方法
public static boolean isOnline() {
    return RongIM.getInstance().getCurrentConnectionStatus().equals(RongIMClient.ConnectionStatusListener.ConnectionStatus.CONNECTED);
}
步骤4: 会话列表界面
就是用IMKit中的ConversationListFragment,可以认为就是个fragment。正常用就会显示,后期会介绍在这fragment中新增些自定义的会话
注意点:
它是用uri来设置你要显示的类型
如果本身就是显示会话列表界面需要在清单中配置(可参考融云文档)
示例代码:
public Fragment initConversationList() {
    if (mListFragment == null) {
        mListFragment = new ConversationListFragment();
        uri = Uri.parse("rong://" + BuildConfig.APPLICATION_ID).buildUpon()
                .appendPath("conversationlist")
                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false")
                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")
                .build();
        mListFragment.setUri(uri);
    }
    return mListFragment;
}
步骤5:会话界面
用的是IMkit中的ConversationFragment。有targetId就能显示,targetId就是对方的IM账号
示例代码:
private void enterFragment(Conversation.ConversationType mConversationType, String toUserIm) {
    fragment = new ConversationFragment();
    uri = Uri.parse("rong://" + BuildConfig.APPLICATION_ID).buildUpon()
            .appendPath("conversation")
            .appendPath(mConversationType.getName().toLowerCase())
            .appendQueryParameter("targetId", toUserIm).build();
    fragment.setUri(uri);
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    //xxx 为你要加载的 id
    transaction.add(R.id.rong_content, fragment);
    transaction.commitAllowingStateLoss();
    showRealView();
}
在 AndroidManifest.xml 中,会话 Activity 下面配置 intent-filter。 注意请修改 android:host 为 App 的 ApplicationId,其他保持不变。
 
 
  
 
  
  
ApplicationId可以不改,在build.gradle里面配置
manifestPlaceholders = [ APPLICATIONID: applicationId]
启动会话界面  
示例代码:
public static void startChattingActivity(Context context, Conversation.ConversationType conversationType, String targetId, String title) {
    if (context != null && !TextUtils.isEmpty(targetId) && conversationType != null) {
        Uri uri = Uri.parse("rong://" + BuildConfig.APPLICATION_ID).buildUpon()
                .appendPath("conversation")
                .appendPath(conversationType.getName().toLowerCase(Locale.US))
                .appendQueryParameter("targetId", targetId)
                .appendQueryParameter("title", title)
             
                .build();
        context.startActivity(new Intent("android.intent.action.VIEW", uri));
    } else {
        throw new IllegalArgumentException();
    }
}
title是用来显示标题的,当然会话界面就需要做获取代码了。
还有混淆啥的,可参考官网
好了,到这里接入IM,并且建立聊天就完成了。有问题可加我QQ:893151960或者群142739277
先说明:解决问题可能收费的,讨论问题免费