WuKongIM Android EasySDK is a lightweight Android SDK that enables you to add real-time chat functionality to your Android application in just 5 minutes. This guide will take you through the complete process from installation to sending your first message.
System Requirements: Android 5.0 (API level 21) or higher, Kotlin 1.5.0 or higher
// 1. Initialize SDKval config = WuKongConfig.Builder() .serverUrl("ws://your-wukongim-server.com:5200") .uid("your_user_id") // Your user ID .token("your_auth_token") // Your authentication token // .deviceId("optional_device_id") // Optional: Device ID // .deviceFlag(WuKongDeviceFlag.APP) // Optional: Device flag, default is APP .build()val easySDK = WuKongEasySDK.getInstance()easySDK.init(this, config) // this is Application or Activity context
In some cases, you may need to remove event listeners to avoid memory leaks or duplicate processing. Android EasySDK provides methods to remove listeners.
Important Reminder: In Android, removing event listeners requires maintaining references to the listeners. It’s recommended to use class properties to store listener references for later removal.
Built-in Auto Reconnection: Android EasySDK has built-in intelligent reconnection mechanism, no need to manually implement reconnection logic. The SDK will automatically attempt to reconnect when the connection is lost.
Copy
// Proper connection status listeningeasySDK.addEventListener(WuKongEvent.CONNECT, object : WuKongEventListener<ConnectResult> { override fun onEvent(result: ConnectResult) { Log.d("WuKong", "Connection successful: $result") // Update UI status, enable sending functionality runOnUiThread { updateConnectionUI(true) } }})easySDK.addEventListener(WuKongEvent.DISCONNECT, object : WuKongEventListener<DisconnectInfo> { override fun onEvent(disconnectInfo: DisconnectInfo) { Log.d("WuKong", "Connection lost: $disconnectInfo") Log.d("WuKong", "Disconnect code: ${disconnectInfo.code}, reason: ${disconnectInfo.reason}") // Update UI status, disable sending functionality runOnUiThread { updateConnectionUI(false) } // SDK will automatically attempt to reconnect, no manual handling needed }})easySDK.addEventListener(WuKongEvent.ERROR, object : WuKongEventListener<WuKongError> { override fun onEvent(error: WuKongError) { Log.e("WuKong", "Error occurred: $error") // Handle based on error type runOnUiThread { when (error.code) { WuKongErrorCode.AUTH_FAILED -> { // Authentication failed, need to get new token handleAuthError() } WuKongErrorCode.NETWORK_ERROR -> { // Network error, show network prompt showNetworkError() } else -> { // Other errors showGeneralError(error.message) } } } }})