Integrate with Android

更新时间:
复制 MD 格式

This topic describes how to integrate the mPaaS switch configuration feature into your Android app.

Switch configuration lets you change client behavior without releasing a new app version. The client pulls switch values from the backend on a configurable schedule, and your code reads those values by key at runtime. A switch is a key-value pair. Switch configuration supports two connection types: native AAR integration and component-based integration.

Using switch configuration calls the MDS update and publish API, which incurs fees. For billing details, see the real-time publishing billing item in Pricing.

The procedure includes three steps:

  1. Add the SDK

  2. Initialize mPaaS (native AAR integration only)

  3. Use the SDK

Prerequisites

Before you begin, ensure that you have:

Add the SDK

Native AAR integration

Use Component Management (AAR) to install the Switch Configuration (CONFIGSERVICE) component in your project. For details, see AAR Component Management.

Component-based integration

In the Portal and Bundle projects, use Component Management to install the Switch Configuration (CONFIGSERVICE) component. For details, see Manage component dependencies.

Initialize mPaaS

Native AAR integration requires explicit initialization. Add the following code to your Application object:

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

For more information, see Initialize mPaaS.

Use the SDK

mPaaS provides the MPConfigService interface for switch configuration management.

Follow these steps to implement switch configuration:

  1. In the mPaaS console, go to Real-time Release > Switch Configuration Management. Add the required switch configuration items, then publish targeted configurations based on platform, whitelist, percentage, version number, device model, and Android version. For details, see Android/iOS Configuration Management.

  2. After publishing a switch in the console, call the MPConfigService APIs in your app to read values and respond to changes.

    The following table describes the available APIs.

    Method

    Description

    getConfig(String key)

    Returns the current value of a switch by key. Returns the last cached value if the client has not yet completed its first sync.

    loadConfig()

    Pulls the latest switches from the server. By default, the client calls this automatically at a 30-minute interval.

    loadConfigImmediately(long delay)

    Triggers an immediate pull. Set delay to 0 to load without waiting, or pass a millisecond value to defer.

    addConfigChangeListener(ConfigService.ConfigChangeListener)

    Registers a listener that fires when a switch value changes. Returns true if registration succeeded.

    removeConfigChangeListener(ConfigService.ConfigChangeListener)

    Unregisters a previously registered listener.

    Important

    The listener is held as a soft reference and may be reclaimed by the system under memory pressure. Register the listener only when needed and unregister it when done — do not use a global listener.

     public class MPConfigService {
         /**
          * Gets a switch.
          *
          * @param key
          * @return
          */
         public static String getConfig(String key);
         /**
          *  Loads switches. By default, the latest switches are pulled from the server-side at a 30-minute interval.
          */
         public static void loadConfig();
         /**
          * Loads switches immediately.
          *
          * @param delay The delay time for loading switches, in milliseconds. A value of 0 means load immediately.
          */
         public static void loadConfigImmediately(long delay);
         /**
          * Registers a switch change listener.
          * @param configChangeListener The listener.
          * @return
          */
         public static boolean addConfigChangeListener(ConfigService.ConfigChangeListener configChangeListener);
         /**
          * Removes a switch change listener.
          * @param configChangeListener The listener.
          */
         public static void removeConfigChangeListener(ConfigService.ConfigChangeListener configChangeListener);
     }