V2TIMManager is a core class and also an entry class of the IM SDK. It implements features such as IM SDK initialization and login, message sending/receiving, group creation, and group leaving. To complete initialization, call the initSDK API.
// 1. Obtain the SDKAppID of the application from the IM console. For more information, see SDKAppID.
// 2. Initialize the `config` object.
V2TIMSDKConfig config = new V2TIMSDKConfig();
// 3. Specify the log output level. For more information, see SDKConfig.
config.setLogLevel(V2TIMSDKConfig.V2TIM_LOG_INFO);
// 4. Initialize the SDK and set the listening object of `V2TIMSDKListener`.
// After you call `initSDK`, the SDK automatically connects to the network. The network connection status can be listened to in the `V2TIMSDKListener` callback.
V2TIMManager.getInstance().initSDK(context, sdkAppID, sdkConfig, new V2TIMSDKListener() {
// 5. Listen to the `V2TIMSDKListener` callback.
@Override
public void onConnecting() {
// The SDK is connecting to the Tencent CVM instance.
}
@Override
public void onConnectSuccess() {
// The SDK is successfully connected to the Tencent CVM instance.
}
@Override
public void onConnectFailed(int code, String error) {
// The SDK fails to connect to the Tencent CVM instance.
}
});
The initialization API initSDK
contains three required parameters, SDKAppID
, SDKConfig
, and listener
.
SDKAppID
is a unique ID that the IM service uses to identify a customer account. We recommend that you apply for a new SDKAppID
for every independent app to automatically isolate messages between SDKAppIDs
.
You can view all SDKAppIDs
in the IM console or click Create an Application to create an SDKAppID
.
The V2TIMSDKConfig
parameter is used for SDK initialization configuration. It is often used to set the log level, that is, the setLogLevel API. The following table lists the log levels:
Log Level | Log Output |
---|---|
V2TIM_LOG_NONE | No log is output. |
V2TIM_LOG_DEBUG | Logs of the DEBUG, INFO, WARNING, and ERROR levels are output. |
V2TIM_LOG_INFO | Logs of the INFO, WARNING, and ERROR levels are output. |
V2TIM_LOG_WARN | Logs of the WARNING and ERROR levels are output. |
V2TIM_LOG_ERROR | Logs of the ERROR level are output. |
/sdcard/tencenet/imsdklogs/<application package="" name="">
directory for versions earlier than 4.8.50 and in the /sdcard/Android/data/<package name="">/files/log/tencent/imsdk
directory for version 4.8.50 or later.python decode_mars_nocrypt_log_file.py imsdk_yyyyMMdd.xlog
V2TIMSDKListener is used to listen to the network status and changes to user information.
Event Callback | Event Description | Recommended Operation |
---|---|---|
onConnecting | The SDK is connecting to the CVM instance. | The "Connecting" status can be displayed on the UI. |
onConnectSuccess | The SDK is successfully connected to the CVM instance. | - |
onConnectFailed | The SDK fails to connect to the CVM instance. | The user can be notified that the network connection is currently unavailable. |
onKickedOffline | The current user is kicked offline. | The "You have already logged in to the SDK on another device using the current account. Are you sure you want to log in again?" message can be displayed on the UI. |
onUserSigExpired | The UserSig expires. | Use the new UserSig for login. |
onSelfInfoUpdated | The information of the current user is updated. | Update your own profile photo and nickname on the UI. |
Note:If you receive the
onUserSigExpired
callback, the UserSig that you use for login has expired. In this case, you need to update the UserSig and then log in again. If you continue to use the expired UserSig, the SDK falls into an endless login loop.
You can call the login(userID, userSig) function of V2TIMManager
to log in to the SDK. The features of the IM SDK are available to you only after you successfully log in to the SDK.
Note:After you log in successfully by calling
IM SDK Login
, DAU will be calculated. Please useIM SDK Login
appropriately according to the business scenario to avoid an excessively high DAU.
login
function in the following scenarios:onUserSigExpired
callback. That is, when the UserSig expires, you need to use the new UserSig for login.onKickOffline
callback. That is, when the current user is kicked offline, the "You have already logged in to the SDK on another device using the current account. Are you sure you want to log in again?" message can be displayed on the UI. In this case, you can select "Yes" to log in again.You do not need to call the login
function in the following scenarios:
login
function as the SDK automatically goes online.You cannot use the same account to log in on two mobile phones of the same model. For example, you cannot use the same account for login on two Apple mobile phones. However, one Android mobile phone and one Apple mobile phone will be considered as two different devices, and you can use the same account to log in on these two devices. For more information on configurations related to multi-device login, see the Login settings section in Feature Configuration.
To log out of the SDK, call the logout function.
Was this page helpful?