Using the SDK

Updated at:

After adding the SDK, complete the following steps to integrate the switch component into your HarmonyOS client:

  1. Initialize the switch component

  2. Configure the grayscale whitelist

  3. Retrieve the switch

  4. Refresh the switch

  5. Register and remove a switch listener

Initialize the switch component

import { ConfigChangeListener, MPConfigService } from '@mpaas/configservice/Index';
···
MPConfigService.init()

Configure the grayscale whitelist

MPFramework.instance.userId = 'mpaas'

Get the switch

MPConfigService.getConfig(your_switch_key)

Refresh the switch

// Triggers a refresh with a 30-minute interval.
MPConfigService.loadConfig()
// Triggers an immediate refresh.
MPConfigService.loadConfigImmediately()

Register and remove a switch listener

// Register a listener.
MPConfigService.addConfigChangeListener("myFilter", new MyListener())
// Remove the listener.
MPConfigService.removeConfigChangeListener("myFilter")


class MyListener implements ConfigChangeListener{
  getKeys(): List<string> {
    let filter = new List<string>()
    filter.add('the_switch_key_to_monitor')
    return filter;
  }

  onConfigChange(key: string, value: string): void {
    // A notification is sent here when the switch value changes.
  }
}