# Contacts (/en/realtime-media/im/build/build-core-messaging/contacts/android)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

After logging in to Chat, users can start adding contacts and chatting with each other. They can also manage these contacts, for example, by adding, retrieving and removing contacts. They can also add the specified user to the blocklist to stop receiving messages from that user.

    Agora Chat, by default, allows two users to send chat messages to each other as strangers. This means that users can chat without adding each other as a contact. If you only allow chat between contacts, you can contact [support@agora.io](mailto\:support@agora.io) to enable the friend relationship check switch. After this function is enabled, the SDK will check whether the two users trying to chat are on the contact list of each other. If no, the SDK will report error code 221.

    Agora Chat, by default, allows two users to send chat messages to each other as strangers. This means that users can chat without adding each other as a contact. If you only allow chat between contacts, you can contact [support@agora.io](mailto\:support@agora.io) to enable the friend relationship check switch. After this function is enabled, the SDK will check whether the two users trying to chat are on the contact list of each other. If no, the SDK will report error code 221.

    This page shows how to use the Chat SDK to implement contact management.

    ## Understand the tech [#understand-the-tech]

    The Chat SDK uses `ContactManager` to add, remove and manage contacts. The following are the core methods:

    * `addContact`: Adds a contact.
    * `acceptInvitation`: Accepts the contact invitation.
    * `declineInvitation`: Declines the contact invitation.
    * `deleteContact`: Deletes a contact.
    * `getAllContactsFromServer`: Retrieves a list of contacts from the server.
    * `addUserToBlackList`: Adds the specified user to the blocklist.
    * `removeUserFromBlackList`: Removes the specified user from the blocklist.
    * `getBlackListFromServer`: Retrieves a list of blocked users from the server.

    ## Prerequisites [#prerequisites]

    Before proceeding, ensure that you meet the following requirements:

    * You have integrated the Chat SDK, initialized the SDK and implemented the functionality of registering accounts and login. For details, see [Chat SDK quickstart](../../get-started-sdk).
    * You understand the API call frequency limits as described in [Limitations](../limitations).

    ## Implementation [#implementation]

    This section shows how to manage contacts with the methods provided by the Chat SDK.

    ### Manage the contact list [#manage-the-contact-list]

    Use this section to understand how to send a contact invitation, listen for contact events, and accept or decline the contact invitation.

    #### Send a contact invitation [#send-a-contact-invitation]

    Call `addContact` to add the specified user as a contact:

    ```java
    //The parameters are the username of the contact to be added and the reason for adding
    ChatClient.getInstance().contactManager().addContact(toAddUsername, reason);
    ```

    #### Listen for contact events [#listen-for-contact-events]

    Use `ContactListener` to add the following callback events. When a user receives a contact invitation, you can accept or decline the invitation.

    ```java
     ChatClient.getInstance().contactManager().setContactListener(new ContactListener() {

                //Occurs when a contact request is approved.
                @Override
                public void onFriendRequestAccepted(String username) { }

                //Occurs when a contact request is rejected.
                @Override
                public void onFriendRequestDeclined(String username) { }

                //Occurs when a contact invitation is received.
                @Override
                public void onContactInvited(String username, String reason) { }

                //Occurs when a contact is deleted.
                @Override
                public void onContactDeleted(String username) { }

                //Occurs when a contact is added.
                @Override
                public void onContactAdded(String username) { }
            });
    ```

    #### Accept or decline the contact invitation [#accept-or-decline-the-contact-invitation]

    After receiving `onContactInvited`, call `acceptInvitation` or `declineInvitation` to accept or decline the invitation.

    ```java
    // Accept the contact invitation
    ChatClient.getInstance().contactManager().acceptInvitation(username);

    // Decline the contact invitation
    ChatClient.getInstance().contactManager().declineInvitation(username);
    ```

    #### Delete a contact [#delete-a-contact]

    Call `deleteContact` to delete the specified contact. The deleted user receives the `onContactDeleted` callback.

    ```java
    ChatClient.getInstance().contactManager().deleteContact(username);
    ```

    #### Retrieve the contact list [#retrieve-the-contact-list]

    To get the contact list, you can call `getAllContactsFromServer` to retrieve contacts from the server. After that, you can also call `getContactsFromLocal` to retrieve contacts from the local database.

    ```java
    // Retrieves a list of contacts from the server
    List usernames = ChatClient.getInstance().contactManager().getAllContactsFromServer();

    // Retrieves a list of contacts from the local database
    List usernames = ChatClient.getInstance().contactManager().getContactsFromLocal
    ```

    ### Manage the block list [#manage-the-block-list]

    You can add a specified user to your block list. Once you do that, you can still send chat messages to that user, but you cannot receive messages from them.

    Users can add any other chat user to their block list, regardless of whether this other user is a contact or not. A contact added to the block list remains in the contact list.

    #### Add a user to the block list [#add-a-user-to-the-block-list]

    Call `addUserToBlackList` to add the specified user to the block list.

    You can add any other users to the block list, regardless of whether they are on the contact list or not. Contacts are still displayed on the contact list even if they are added to the block list. After adding users to the block list, you can still send messages to them, but will not receive messages from them as they cannot send messages or friend requests to you.

    ```java
    // The effect of true and false is the same.
    ChatClient.getInstance().contactManager().addUserToBlackList(username,true);
    ```

    #### Remove a user from the block list [#remove-a-user-from-the-block-list]

    To remove the specified user from the block list, call `removeUserFromBlackList`.

    ```java
    ChatClient.getInstance().contactManager().removeUserFromBlackList(username);
    ```

    #### Retrieve the block list from the server [#retrieve-the-block-list-from-the-server]

    To get the block list, call `getBlackListFromServer` to retrieve a list of blocked users from the server.

    ```java
    ChatClient.getInstance().contactManager().getBlackListFromServer();
    ```

    After retrieving the block list from the server, you can also call `getBlackListUsernames` to retrieve the block list from the local database.

    ```java
    ChatClient.getInstance().contactManager().getBlackListUsernames();
    ```

    
  
      
  
      
  
      
  
      
  
      
  
      
  
