Android

更新时间:
复制 MD 格式

mPaaS provides the configuration management service (ConfigService) for implementing switch configuration for AB Test.

mPaaS provides the configuration management service (ConfigService) for implementing switch configuration for AB Test.

  • The configuration management service provides the immediate-pull API and configuration change listening logic so that configuration changes can be refreshed immediately.

  • Configurations are pulled every half an hour by default.

The configuration management service is a common Service component and used in the same way as other Service components defined by users. For details about the Service component, see Register a Service component.

Procedure

  1. On the mPaaS console, choose Delivery Service > Manage configuration , add required configuration items, and set up configuration based on information including the platform, whitelist, percentage, version number, device model, and Android version. For details, see Manageg configurations.

  2. After configurations are released in Delivery Service on the mPaaS console, the client can call the API to obtain key values corresponding to the switch configuration.

The configuration management service provides many APIs externally. You can understand the function of each API according to its name. The common APIs are listed:

// Obtain a value based on a key. All key-value pairs are string type.
String getConfigForAB(String key, String spm)
// Trigger configuration loading. Configurations are loaded only when the interval of half an hour is reached, rather than being loaded immediately.
void loadConfig()
// Immediately request the latest configurations from the server.
loadConfigImmediately(long delay)
// Transfer the userId parameter to ConfigService after login. The whitelist is valid only after userId is transferred.
refreshAfterLogin(String userId)
// Listen to configuration changes.
addConfigChangeListener(ConfigChangeListener configChangeListener)

Code sample

public class MainActivity extends Activity {

    private ConfigService configService = (ConfigService) LauncherApplicationAgent.getInstance().getMicroApplicationContext().findServiceByInterface(ConfigService.class.getName());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        configService.refreshAfterLogin("mPaaSAndroidTester");
        configService.loadConfig();
        setContentView(com.mpaas.demo.ABtest.launcher.R.layout.main);
        findViewById(R.id.btn_trigger).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String target = configService.getConfigForAB("test0515", "MPABTest");
                Toast.makeText(MainActivity.this, String.format("target: %s", target), Toast.LENGTH_LONG).show();
                MPLogger.uploadAll();
            }
        });

    }
}