All Products
Search
Document Center

Access Android

Last Updated: Apr 07, 2021
Important: Since June 28, 2020, mPaaS has stopped support for the baseline 10.1.32. Please use 10.1.68 or 10.1.60 instead. For how to upgrade the baseline from version 10.1.32 to 10.1.68 or 10.1.60, see mPaaS 10.1.68 upgrade guide or mPaaS 10.1.60 upgrade guide.

This topic briefly describes how to fast integrate MSS to the Android client. You can access MAS through Native AAR, mPaaS Inside, or Portal & Bundle.

The complete access process mainly includes the following 2 steps:

  1. Add SDK
  2. Use SDK

Prerequisites

You have integrated mPaaS to your project.

Add SDK

Native AAR mode

Follow the instructions in AAR component management to install the SYNC component in the project through Component management (AAR).

mPaaS Inside mode

Install the SYNC component in the project through Component management (AAR).

Fore more information, see Manage component dependencies > Add/delete component dependencies.

Componentized access mode

Install the SYNC component in the Portal and Bundle projects through Component management (AAR).

Fore more information, see Manage component dependencies > Add/delete component dependencies.

Use SDK

In baseline 10.1.32 or later version, the MPSync class at the mPaaS middle layer encapsulates all APIs of MSS. You can have a quick glance of these APIs in the following table. For more information about the APIs, see Android API reference.

API Description
setup(Application application) Initializes basic services on which MSS depends. Call this API before you call the initialize method. This API is available only in baseline 10.1.60 and later versions.
initialize(Context context) Initializes APIs and MSS.
appToForeground() Notifies the client SDK that the App has been switched to the foreground and it needs to connect to the server. Call this API every time the App is switched to the foreground.
appToBackground() Notifies the client SDK that the 128344 has been switched to the background and it needs to disconnect from the server. Call this API every time the App is switched to the background.
updateUserInfo(String sessionId) Call this API when the login information (userId or sessionId) is changed. This API is called at least once.
clearUserInfo() Clears user information when a user logs out.
registerBiz(String bizType, ISyncCallback syncCallback) You can call this API to register a callback to receive business data. If this API is called, the client SDK will call the syncCallback class after receiving synchronized data.
unregisterBiz(String bizType) Unregisters a specified synchronization configuration. If this API is called, the client SDK will not call the syncCallback class when receiving synchronized data.
reportMsgReceived(SyncMessage syncMessag) After the data is received in the syncCallback implementation class, this API is called to notify MSS that the sync data has been received. Before receiving the reportMsgReceived message, MSS attempts to resend the data for a maximum of six times. If all resending attempts fail, the data will be permanently deleted.
isConnected() Checks whether MSS is running properly.

Code sample

This sample is based on the mPaaS SDK 10.1.32 baseline. The example App provides a button. When a user taps this button, MSS obtains the device ID, and pushes the sync data to the target device specified in the console based on the device ID. In this sample, the sync ID is bizType.

Note: This sample is only intended for demonstrating how to call MSS APIs, and is not the best practice of MSS. You can get the best practice code of MSS from Obtain code samples.
  1. public void button1Clicked(View view)
  2. {
  3. // Obtain the device ID using the getUtdid method.
  4. String utdid =UTDevice.getUtdid(MainActivity.this);
  5. // Print mobile sync data in Logcat.
  6. Log.e("=========",utdid);
  7. // Initialize the API and MSS.
  8. MPSync.initialize(MainActivity.this);
  9. // Register a callback for receiving service data. If this API is called, the client SDK will call the syncCallback class after receiving synchronized data.
  10. MPSync.registerBiz("bizType",new SyncCallBackImpl());
  11. // Set up a network connection with the server.
  12. MPSync.appToForeground();
  13. }
  14. public class SyncCallBackImpl implements ISyncCallback
  15. {
  16. @Override
  17. public void onReceiveMessage(SyncMessage syncMessage) {
  18. //Print mobile sync data in Logcat.
  19. Log.e("=========",syncMessage.msgData);
  20. // Notify the MSS server that the sync data has been received.
  21. MPSync.reportMsgReceived(syncMessage);
  22. }
  23. }

What to do next