适用于 Android 的 App Configuration 客户端 SDK
App Configuration 服务提供 Android 客户端 SDK,用于与以 Kotlin 或 Java 编程语言编写的 Android 应用程序集成。
先决条件
以下是使用 App Configuration 服务 SDK for Android 的先决条件:
- Android API 级别 22 或更高版本
- Android Studio
- Gradle
集成以 Kotlin 编写的客户端 SDK for Android 应用程序
App Configuration 服务提供 Android 客户端 SDK 以与 Android 应用程序集成。 您可以通过集成 SDK 来评估属性和功能部件标志的值。
-
使用下列其中一个选项来安装 SDK:
- 下载 并将软件包导入到 Android Studio 项目。
- 通过 Gradle 获取包,方法是添加以下内容:
-
将 App Configuration Android 客户端 SDK 依赖关系添加到项目级别
build.gradle
文件。repositories { mavenCentral() }
-
将 App Configuration Android 客户端 SDK 依赖关系添加到模块级别
build.gradle
文件。dependencies { implementation "com.ibm.cloud:appconfiguration-android-sdk:0.3.1" }
-
-
配置
AndroidManifest.xml
文件以获取因特网许可权。<uses-permission android:name="android.permission.INTERNET"/>
-
初始化 SDK。
import com.ibm.cloud.appconfiguration.android.sdk.AppConfiguration val collectionId = "airlines-webapp" val environmentId = "dev" val appConfigClient = AppConfiguration.getInstance() //application is a member of the AppCompatActivity() class, if you have inherited the //AppCompatActivity() class then you can call the application variable. appConfigClient.init(application, "region", "guid", "apikey") appConfigClient.setContext(collectionId, environmentId)
其中:
region
- 创建 App Configuration 服务实例的区域名称。 点击此处 查看支持的地点列表。 例如:-us-south
,au-syd
等。guid
- App Configuration 服务的 GUID。 从仪表板的服务凭证部分获取。apikey
- App Configuration 服务的 ApiKey。 从仪表板的服务凭证部分获取。collectionId
-在“集合”部分下的 App Configuration 服务实例中创建的集合的标识。environmentId
-在“环境”部分下的 App Configuration 服务实例中创建的环境的标识。
-
设置功能部件或属性数据更改的侦听器
appConfiguration.registerConfigurationUpdateListener(object : ConfigurationUpdateListener { override fun onConfigurationUpdate() { // ADD YOUR CODE } })
使用以 Kotlin 编写的 Android 应用程序的属性和功能相关 API 的示例
-
获取单一功能
val feature: Feature? = appConfiguration.getFeature("featureId")
-
获取所有功能
val features: HashMap<String, Feature>? = appConfiguration.getFeatures();
-
功能评估
您可以使用
feature.getCurrentValue()
方法来评估功能标志的值。 传递唯一的entityId
作为参数以执行功能标志求值。 如果使用 App Configuration 服务中的段配置了功能标志,那么可以将属性值设置为 JSONObject。JSONObject entityAttributes = new JSONObject(); try { entityAttributes.put("city", "Bangalore"); entityAttributes.put("country", "India"); } catch (JSONException e) { e.printStackTrace(); } val appConfiguration = AppConfiguration.getInstance() val feature: Feature? = appConfiguration.getFeature("featureId") if (feature?.getFeatureDataType() === Feature.FeatureType.NUMERIC) { val value = feature.getCurrentValue("entityId", entityAttributes) } else if (feature?.getFeatureDataType() === Feature.FeatureType.BOOLEAN) { val value = feature.getCurrentValue("entityId", entityAttributes) } else if (feature?.getFeatureDataType() === Feature.FeatureType.STRING) { val value = feature.getCurrentValue("entityId", entityAttributes) }
-
获取单个属性
val property: Property? = appConfiguration.getProperty("propertyId")
-
获取所有属性
val properties: HashMap<String, Property>? = appConfiguration.getProperties();
-
属性评估
您可以使用
property.getCurrentValue()
方法来评估属性的值。 传递唯一的entityId
作为参数以执行属性求值。 如果使用 App Configuration 服务中的段配置该属性,那么可以将属性值设置为 JSONObject。JSONObject entityAttributes = new JSONObject(); try { entityAttributes.put("city", "Bangalore"); entityAttributes.put("country", "India"); } catch (JSONException e) { e.printStackTrace(); } val appConfiguration = AppConfiguration.getInstance() val property: Property? = appConfiguration.getProperty("propertyId") val value = property.getCurrentValue("entityId", entityAttributes)
支持的数据类型
App Configuration 服务在以下数据类型中配置功能标志和属性 :Boolean,Numeric 和 String。 字符串数据类型可以是文本字符串,JSON 或 YAML 的格式。 因此,SDK 处理每种格式,如表 1 中所示。
功能部件或属性值 | 数据类型 | 数据格式 | getCurrentValue() 返回的数据类型 |
示例输出 |
---|---|---|---|---|
true |
BOOLEAN | 不适用 | java.lang.Boolean |
true |
25 |
NUMERIC | 不适用 | java.lang.Integer |
25 |
"a string text" | STRING | TEXT | java.lang.String |
a string text |
{"firefox": { "name": "Firefox", "pref_url": "about:config" }} |
字符串 | JSON | org.json.JSONObject |
{"firefox":{"name":"Firefox","pref_url":"about:config"}} |
men: - John Smith - Bill Jones women: - Mary Smith - Susan Williams |
STRING | YAML | java.lang.String |
`"men:
|
功能标志
val feature: Feature? = appConfiguration.getFeature("json-feature")
feature.getFeatureDataType(); // STRING
feature.getFeatureDataFormat(); // JSON
// Example below (traversing the returned JSONObject)
if (feature != null) {
val result = feature.getCurrentValue(entityId, entityAttributes) as JSONObject
result.get("key") // returns the value of the key
}
val feature: Feature? = appConfiguration.getFeature("yaml-feature")
feature.getFeatureDataType(); // STRING
feature.getFeatureDataFormat(); // YAML
feature.getCurrentValue(entityId, entityAttributes); // returns the stringified yaml (check Table 1)
属性
val property: Property? = appConfiguration.getProperty("json-property")
property.getPropertyDataType(); // STRING
property.getPropertyDataFormat(); // JSON
// Example below (traversing the returned JSONObject)
if (property != null) {
val result = property.getCurrentValue(entityId, entityAttributes) as JSONObject
result.get("key") // returns the value of the key
}
val property: Property? = appConfiguration.getProperty("yaml-property")
property.getPropertyDataType(); // STRING
property.getPropertyDataFormat(); // YAML
property.getCurrentValue(entityId, entityAttributes); // returns the stringified yaml (check above Table 1)
-
强制从服务器访存配置。
appConfiguration.fetchConfigurations()
集成以 Java 编写的客户端 SDK for Android 应用程序
App Configuration 服务提供 Android 客户端 SDK 以与 Android 应用程序集成。 您可以通过集成 SDK 来评估属性和功能部件标志的值。
-
使用下列其中一个选项来安装 SDK:
- 下载 并将软件包导入到 Android Studio 项目。
- 通过添加以下内容,通过 Gradle 获取包:
-
将 App Configuration Android 客户端 SDK 依赖关系添加到项目级别
build.gradle
文件。repositories { mavenCentral() }
-
将 App Configuration Android 客户端 SDK 依赖关系添加到模块级别
build.gradle
文件。dependencies { implementation "com.ibm.cloud:appconfiguration-android-sdk:0.3.1" }
-
-
配置
AndroidManifest.xml
文件以获取因特网许可权。<uses-permission android:name="android.permission.INTERNET"/>
-
通过以下步骤将 Kotlin 集成到 Java 项目:
-
将 Kotlin Gradle 插件添加到模块级别
build.gradle
dependencies { classpath "com.android.tools.build:gradle:4.1.1" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
-
将
kotlin-android
插件添加到应用程序级别build.gradle
plugins { id 'com.android.application' id 'kotlin-android' }
-
-
初始化 SDK。
AppConfiguration appConfiguration = AppConfiguration.getInstance(); appConfiguration.init(getApplication(), "region", "guid", "apikey"); // To start the configuration fetching operation, set the collectionId in the following way. appConfiguration.setContext("collectionId", "environmentId");
其中:
region
-在其中创建服务实例的区域名称。 达拉斯使用AppConfiguration.REGION_US_SOUTH
,华盛顿特区使用AppConfiguration.REGION_US_EAST
,伦敦使用AppConfiguration.REGION_EU_GB
,悉尼使用AppConfiguration.REGION_AU_SYD
。guid
- App Configuration 服务的 GUID。 从仪表板的服务凭证部分获取。apikey
- App Configuration 服务的 ApiKey。 从仪表板的服务凭证部分获取。collectionId
-在 App Configuration 服务实例中创建的集合的标识。environmentId
-在“环境”部分下的 App Configuration 服务实例中创建的环境的标识。
-
侦听功能部件更改
appConfiguration.registerConfigurationUpdateListener(new ConfigurationUpdateListener() { @Override public void onConfigurationUpdate() { // ADD YOUR CODE } });
使用以 Java 编写的 Android 应用程序的属性和功能相关 API 的示例
请参阅使用属性和功能相关 API 的示例。
-
获取单一功能
Feature feature = appConfiguration.getFeature("featureId");
-
获取所有功能
HashMap<String,Feature> features = appConfiguration.getFeatures();
-
功能评估
您可以使用
feature.getCurrentValue()
方法来评估功能标志的值。 传递唯一的entityId
作为参数以执行功能标志求值。 如果使用 App Configuration 服务中的段配置了功能标志,那么可以将属性值设置为 JSONObject。JSONObject entityAttributes = new JSONObject(); try { entityAttributes.put("city", "Bengaluru"); entityAttributes.put("country", "India"); } catch (JSONException e) { e.printStackTrace(); } AppConfiguration appConfiguration = AppConfiguration.getInstance(); Feature feature = appConfiguration.getFeature("featureId") if(feature != null) switch (feature.getFeatureDataType()) case STRING: String value = (String) feature.getCurrentValue(entityId, entityAttributes); System.out.println(value); break; case BOOLEAN: Boolean boolVal = (Boolean) feature.getCurrentValue(entityId, entityAttributes); System.out.println(boolVal); break; case NUMERIC: Integer intVal = (Integer) feature.getCurrentValue(entityId, entityAttributes); System.out.println(intVal); break; } }
-
获取单个属性
Property property = appConfiguration.getProperty("propertyId");
-
获取所有属性
HashMap<String,Property> properties = appConfiguration.getProperties();
-
属性评估
您可以使用
property.getCurrentValue()
方法来评估属性的值。 传递唯一的entityId
作为参数以执行属性求值。 如果使用 App Configuration 服务中的段配置该属性,那么可以将属性值设置为 JSONObject。JSONObject entityAttributes = new JSONObject(); try { entityAttributes.put("city", "Bengaluru"); entityAttributes.put("country", "India"); } catch (JSONException e) { e.printStackTrace(); } AppConfiguration appConfiguration = AppConfiguration.getInstance(); Property property = appConfiguration.getProperty("propertyId"); String value = (String) property.getCurrentValue(entityId, entityAttributes);
支持的数据类型
App Configuration 服务在以下数据类型中配置功能标志和属性 :Boolean,Numeric 和 String。 字符串数据类型可以是文本字符串,JSON 或 YAML 的格式。 SDK 相应地处理每种格式,如表 1 中所示。
功能部件标志
Feature feature = appConfiguration.getFeature("json-feature");
feature.getFeatureDataType(); // STRING
feature.getFeatureDataFormat(); // JSON
// Example below (traversing the returned JSONObject)
if (feature != null) {
JSONObject result = (JSONObject) feature.getCurrentValue(entityId, entityAttributes);
result.get("key") // returns the value of the key
}
Feature feature = appConfiguration.getFeature("yaml-feature");
feature.getFeatureDataType(); // STRING
feature.getFeatureDataFormat(); // YAML
feature.getCurrentValue(entityId, entityAttributes); // returns the stringified yaml (check above Table 1)
属性
Property property = appConfiguration.getProperty("json-property");
property.getPropertyDataType(); // STRING
property.getPropertyDataFormat(); // JSON
// Example below (traversing the returned JSONObject)
if (property != null) {
JSONObject result = (JSONObject) property.getCurrentValue(entityId, entityAttributes);
result.get("key") // returns the value of the key
}
Property property = appConfiguration.getProperty("yaml-property");
property.getPropertyDataType(); // STRING
property.getPropertyDataFormat(); // YAML
property.getCurrentValue(entityId, entityAttributes); // returns the stringified yaml (check Table 1)
-
强制从服务器访存配置。
appConfiguration.fetchConfigurations()