hyphenate_SDK4.0 4.2.3-lite
hyphenate java IM SDK
Public Member Functions | List of all members
com.hyphenate.chat.EMClient Class Reference

Public Member Functions

void init (Context context, EMOptions options)
 
void createAccount (String username, String password) throws HyphenateException
 
void login (String id, String password, @NonNull final EMCallBack callback)
 
void loginWithToken (String username, String token, @NonNull final EMCallBack callback)
 
void notifyTokenExpired (String response)
 
void renewToken (String newToken, @NonNull EMCallBack callback)
 
int logout (boolean unbindToken)
 
void logout (final boolean unbindToken, final EMCallBack callback)
 
void addConnectionListener (final EMConnectionListener listener)
 
void removeConnectionListener (final EMConnectionListener listener)
 
void addLogListener (final EMLogListener listener)
 
void removeLogListener (final EMLogListener listener)
 
EMChatRoomManager chatroomManager ()
 
EMChatManager chatManager ()
 
Context getContext ()
 
synchronized String getCurrentUser ()
 
boolean isLoggedInBefore ()
 
boolean isConnected ()
 
boolean isLoggedIn ()
 
void setDebugMode (boolean debugMode)
 
void uploadLog (EMCallBack callback)
 
EMOptions getOptions ()
 
String compressLogs () throws HyphenateException
 
String getAccessToken ()
 
boolean isSdkInited ()
 

Detailed Description

The chat client class, which is the entry of the chat SDK. It defines how to log in to and log out of the chat app and how to manage the connection between the SDK and the chat server.

EMChatManager chatManager = EMClient.getInstance().chatManager();

Member Function Documentation

◆ addConnectionListener()

void com.hyphenate.chat.EMClient.addConnectionListener ( final EMConnectionListener  listener)

Adds a connection status listener.

The listener listens for the connection between the chat app and the chat server.

Parameters
listenerThe connection status listener to add.

◆ addLogListener()

void com.hyphenate.chat.EMClient.addLogListener ( final EMLogListener  listener)

Adds the log callback listener of SDK.

Parameters
listenerThe log callback listener, EMLogListener#onLog(String).

◆ chatManager()

EMChatManager com.hyphenate.chat.EMClient.chatManager ( )

Gets the ChatManager class.

This method can be called only after the EMClient is initialized. See init(Context, EMOptions).

Returns
The ChatManager class.

◆ chatroomManager()

EMChatRoomManager com.hyphenate.chat.EMClient.chatroomManager ( )

Gets the GroupManager class.

This method can be called only after the EMClient is initialized. See init(Context, EMOptions).

Returns
The GroupManager class.

Gets the PushManager class.

This method can be called only after the EMClient is initialized. See init(Context, EMOptions).

Returns
The PushManager class.

Gets the ChatRoomManager class.

This method can be called only after the EMClient is initialized. See init(Context, EMOptions).

Returns
The ChatRoomManager class.

◆ compressLogs()

String com.hyphenate.chat.EMClient.compressLogs ( ) throws HyphenateException

Compresses the debug log file into a gzip archive.

It is strongly recommended that you delete this debug archive as soon as this method is called.

Returns
The path of the compressed gz file.
Exceptions
HyphenateExceptionA description of the cause of the exception if the method fails.

◆ createAccount()

void com.hyphenate.chat.EMClient.createAccount ( String  username,
String  password 
) throws HyphenateException

Adds a new user account.

This method is not recommended and you are advised to call the RESTful API.

This is a synchronous method and blocks the current thread.

Parameters
usernameThe user ID. The maximum length is 64 characters. Ensure that you set this parameter. The user ID can contain characters of the following types:
  • 26 English letters (a-z).
  • 10 numbers (0-9).
  • "_", "-", "." The user ID is case-insensitive, so Aa and aa are the same user ID. The email address or the UUID of the user cannot be used as the user ID. You can also set this parameter with the regular expression ^[a-zA-Z0-9_-]+$.
passwordThe password. The maximum length is 64 characters. Ensure that you set this parameter.
Exceptions
HyphenateExceptionA description of the issue that caused this call to fail. For example, the user account or password is null, or the account is illegal.

◆ getAccessToken()

String com.hyphenate.chat.EMClient.getAccessToken ( )

Gets the access token from the memory.

When uploading or downloading an attachment (a voice, image, or file), you must add the token to the request header. The SDK returns null when any exception occurs.

If the token is null, you can check the EMLog file for the possible reason.

You can also get the token from the server by calling EMOptions#getAccessToken(boolean) and passing true.

Returns
The access token.

◆ getContext()

Context com.hyphenate.chat.EMClient.getContext ( )

Gets the EMUserInfoManager class.

This method can be called only after the EMClient is initialized. See init(Context, EMOptions).

Returns
The EMUserInfoManager class.

Gets the ContactManager class.

This method can be called only after the EMClient is initialized. See init(Context, EMOptions).

Returns
The ContactManager class.

◆ getCurrentUser()

synchronized String com.hyphenate.chat.EMClient.getCurrentUser ( )

Gets the user ID of the current logged-in user.

Returns
The user ID of the current logged-in user.

◆ getOptions()

EMOptions com.hyphenate.chat.EMClient.getOptions ( )

Gets configuration options of the SDK.

◆ init()

void com.hyphenate.chat.EMClient.init ( Context  context,
EMOptions  options 
)

Initializes the SDK.

Make sure to initialize the SDK in the main thread.

Parameters
contextThe context. Make sure to set the parameter.
optionsThe configuration options. Make sure to set the parameter. See EMOptions.

◆ isConnected()

boolean com.hyphenate.chat.EMClient.isConnected ( )

Checks whether the SDK is connected to the chat server.

Returns
Whether the SDK is connected to the chat server.
  • true: Yes;
  • false: No.

◆ isLoggedIn()

boolean com.hyphenate.chat.EMClient.isLoggedIn ( )

Checks whether the user has logged in to the Chat app.

Returns
Whether the user has logged in to the Chat app.
  • true: Yes;
  • false: No.

◆ isLoggedInBefore()

boolean com.hyphenate.chat.EMClient.isLoggedInBefore ( )

Checks whether the user has logged in before.

This method always returns true if you log in successfully and have not called the logout() method yet.

If you need to check whether the SDK is connected to the server, call isConnected().

if(EMClient.getInstance().isLoggedInBefore()){
// Enter the main activity.
}else{
// Enter the login activity.
}
Returns
Whether the user has logged in before.
  • true: The user has logged in before;
  • false: The user has not logged in before or has called the logout() method.

◆ isSdkInited()

boolean com.hyphenate.chat.EMClient.isSdkInited ( )

Checks whether the SDK is initialized.

Returns
Whether the SDK is initialized:
  • true: Yes.
  • false: No.

◆ login()

void com.hyphenate.chat.EMClient.login ( String  id,
String  password,
@NonNull final EMCallBack  callback 
)

Logs in to the chat server with a password.

Also, you can call loginWithToken(String, String, EMCallBack) to log in to the chat server with the user ID and token.

This is an asynchronous method.

Parameters
idThe unique chat user ID. Make sure to set the parameter.
passwordThe password. Make sure to set the parameter.
callbackThe login callback. Make sure to set the parameter. The login result is returned via the callback.

◆ loginWithToken()

void com.hyphenate.chat.EMClient.loginWithToken ( String  username,
String  token,
@NonNull final EMCallBack  callback 
)

Logs in to the chat server with the user ID and token.

This method supports automatic login.

You can also call login(String, String, EMCallBack) to log in to the chat server with the user ID and password.

This is an asynchronous method.

Parameters
usernameThe user ID. Make sure to set the parameter.
tokenThe user token. Make sure to set this parameter.
callbackThe login callback. Make sure to set the parameter. Also, this parameter cannot be null. The result of login is returned via the callback.

◆ logout() [1/2]

int com.hyphenate.chat.EMClient.logout ( boolean  unbindToken)

Logs out of the chat app.

This is a synchronous method and blocks the current thread.

Parameters
unbindTokenWhether to unbind the token upon logout.
  • true: Yes.
  • false: No.
Returns
The logout result.
  • If success, the SDK returns EMError#EM_NO_ERROR if the user successfully logs out;
  • If a failure occurs, the SDK returns the description of the cause to the failure. See EMError.

◆ logout() [2/2]

void com.hyphenate.chat.EMClient.logout ( final boolean  unbindToken,
final EMCallBack  callback 
)

Logs out of the chat server.

This is an asynchronous method.

Parameters
unbindTokenWhether to unbind the token upon logout.
  • true: Yes;
  • false: No.
callbackThe completion callback, which contains the error message if the method call fails.

◆ notifyTokenExpired()

void com.hyphenate.chat.EMClient.notifyTokenExpired ( String  response)

Notifies that the token expires.

The SDK triggers the token expiry notification callback via connectionListener.

Parameters
responseThe request result, which includes the description of the issue that cause the method fails.

◆ removeConnectionListener()

void com.hyphenate.chat.EMClient.removeConnectionListener ( final EMConnectionListener  listener)

Removes the connection status listener.

Parameters
listenerThe connection status listener to remove.

◆ removeLogListener()

void com.hyphenate.chat.EMClient.removeLogListener ( final EMLogListener  listener)

Removes the log callback listener.

Parameters
listenerThe log callback listener.

◆ renewToken()

void com.hyphenate.chat.EMClient.renewToken ( String  newToken,
@NonNull EMCallBack  callback 
)

Renews the Agora token.

If you log in with a token and are notified by a callback method EMConnectionListener that the token is to be expired, you can call this method to update the token to avoid unknown issues caused by an invalid token.

Parameters
newTokenThe new token.
callbackrenew token callback. Make sure to set the parameter. The result of renew token is returned via the callback.

◆ setDebugMode()

void com.hyphenate.chat.EMClient.setDebugMode ( boolean  debugMode)

Sets whether to output the debug information.

This method can be called only after the EMClient is initialized. See init(Context, EMOptions).

Parameters
debugModeWhether to output the debug information:
  • true: Yes;
  • false: No.

◆ uploadLog()

void com.hyphenate.chat.EMClient.uploadLog ( EMCallBack  callback)

Uploads local logs.

The debug logs provide reference for our engineers to fix errors and improve system performance.

This is a synchronous method and blocks the current thread.

Parameters
callbackReserved parameter.

The documentation for this class was generated from the following file: