All Products
Search
Document Center

Mobile Platform as a Service:Android

Last Updated:Nov 17, 2023

This topic briefly describes how to fast integrate the switch configuration function to the Android client.

The switch configuration service enables a client to dynamically modify the processing logic in the client code without releasing a new client version. The client controls related processing based on the switch value dynamically obtained from the backend. With the switch configuration service, you can configure, modify, and push various switches. A switch is a key-value pair.

Currently, you can access switch configuration function through Native AAR or Portal & Bundle.

The complete access process mainly includes the following 3 steps:

  1. Add SDK

  2. Initialize mPaaS (only required for Native AAR)

  3. Use the SDK

Prerequisites

Add SDK

Native AAR mode

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

Componentized access mode

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

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

Initialize mPaaS

If you access MAS through Native AAR, you must initialize mPaaS.

Add the following codes in the object Application:

public class MyApplication extends Application {
    
    @Override
    public void onCreate() {
        super.onCreate();
        // mPaaS initialization
        MP.init(this);    
    }
}

For more details, see Initialize mPaaS.

Use the SDK

mPaaS provides the switch configuration management API MPConfigService to implement switch configuration.

Complete the following steps to implement switch configuration:

  1. In the mPaaS console, go to the Mobile Delivery Service > Manage configuration page, add desired switch configuration items, and set them based on information such as the platform, whitelist, percentage, version number, model, and Android version. For more information, see Configuration management.

  2. After a switch key is released in the console, the client can call the specified API to obtain the value of the switch key.

    The switch configuration management API MPConfigService provides many APIs externally. You can understand the function of each API based on its name. A list of APIs is provided as follows:

     public class MPConfigService {
         /**
          * Obtain switch configurations
          *
          * @param key
          * @return
          */
         public static String getConfig(String key);
         /**
          *  Load switch configurations. The latest switch configurations are obtained every half an hour by default.
          */
         public static void loadConfig();
         /**
          * Load switch configurations immediately.
          *
          * @param delay Delay after which switch configurations are loaded, in ms. 0 indicates that switch configurations are loaded immediately.
          */
         public static void loadConfigImmediately(long delay);
         /**
          * Register a listener for listening to switch configuration changes.
          * @param configChangeListener listener
          * @return
          */
         public static boolean addConfigChangeListener(ConfigService.ConfigChangeListener configChangeListener);
         /**
          * Remove the listener for listening to switch configuration changes
          * @param configChangeListener listener
          */
         public static void removeConfigChangeListener(ConfigService.ConfigChangeListener configChangeListener);
     }
    Important

    As a soft reference object, the listener will be reclaimed when the system has run out of memory. Therefore, please perform switch monitoring instead of global monitoring; meanwhile, register the listener at regular intervals and remove it after use.