Android接环信仿微信App

作者: 奔跑吧李博 | 来源:发表于2020-10-11 11:08 被阅读0次

    国际惯例,先上效果图

    Github代码地址
    环信SDK开发文档
    部分代码

    Application中初始化:

            var options = EMOptions()
            // 默认添加好友时,是不需要验证的,改成需要验证
            options.acceptInvitationAlways = false
            // 是否自动将消息附件上传到环信服务器,默认为True是使用环信服务器上传下载,如果设为 false,需要开发者自己处理附件消息的上传和下载
            options.autoTransferMessageAttachments = true
            // 是否自动下载附件类消息的缩略图等,默认为 true 这里和上边这个参数相关联
            options.setAutoDownloadThumbnail(true)
            //初始化
            EMClient.getInstance().init(applicationContext, options)
            //在做打包混淆时,关闭debug模式,避免消耗不必要的资源
            EMClient.getInstance().setDebugMode(true)
    

    链接状态监听listener :

    open class ConnectionListener : EMConnectionListener {
    
        override fun onConnected() {
            Log.i("minfo", "账号连接成功")
        }
    
        override fun onDisconnected(errorCode: Int) {
            when(errorCode) {
                EMError.USER_REMOVED ->
                    Log.i("minfo", "用户已被删除")
                EMError.USER_LOGIN_ANOTHER_DEVICE ->
                    Log.i("minfo", "异地登录被挤掉")
    
            }
        }
    
    }
    

    账号注册:

            //注意:这里的注册有个坑,必须要在子线程中才可以,否则会抛出异常
            Thread {
                kotlin.run {
                    //注册账号,注册失败会抛出HyphenateException: Registration failed.
                    //比如重复注册,比如在子线程中注册
                    try {
                        EMClient.getInstance().createAccount(userId, psd)  //同步方法
                    } catch (e: Exception) {
                        Log.i("minfo", "账号已存在")
                    }
    
                    runOnUiThread {
                        //账号登录
                        login()
                    }
                }
            }.start()
    

    登录:

            EMClient.getInstance().login(userId, userPsd, object: EMCallBack {
                override fun onSuccess() {
                    Log.i("minfo", "登录聊天服务器成功")
                    startActivity(Intent(this@LoginActivity, MainActivity::class.java))
                }
    
                override fun onProgress(progress: Int, status: String?) {
    
                }
    
                override fun onError(code: Int, error: String?) {
                    Log.i("minfo", "登录聊天服务器失败")
                }
    
            })
    

    退出登录:

            EMClient.getInstance().logout(true, object: EMCallBack {
                override fun onSuccess() {
                    Log.i("minfo", "退出登录成功")
                }
    
                override fun onProgress(progress: Int, status: String?) {
    
                }
    
                override fun onError(code: Int, error: String?) {
                    Log.i("minfo", "退出登录失败")
                }
    
            })
    

    跳转聊天页:

            val chatFragment = EaseChatFragment()
            val args = Bundle()
            args.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE)
            args.putString(EaseConstant.EXTRA_USER_ID, userId)
            chatFragment.arguments = args
            supportFragmentManager.beginTransaction().add(R.id.flChat, chatFragment).commit()
    

    相关文章

      网友评论

        本文标题:Android接环信仿微信App

        本文链接:https://www.haomeiwen.com/subject/fecvuktx.html