背景:
现在AGC远程配置端侧服务提供的SDK支持传入自定义属性获取和更新云端配置数据了。下面将通过一个demo集成远程配置SDK来实现这一功能。
集成准备
1.在AGC创建工程并开通远程配置服务。
2.在Android Studio中创建一个工程,将agconnect-services.json文件拷贝到项目的app目录下。

3.在项目级build.gradle中配置Maven仓地址和AGC插件地址。
buildscript {
    repositories {
        google()
        jcenter()
        maven { url 'https://developer.huawei.com/repo/' }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.0"
        classpath 'com.huawei.agconnect:agcp:1.6.5.200'
    }
}
allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://developer.huawei.com/repo/' }
    }
}
4.在应用级build.gradle中添加编译依赖和集成SDK。
apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
dependencies {
…
implementation 'com.huawei.agconnect:agconnect-remoteconfig:1.6.5.200'
compileOnly 'com.huawei.agconnect:agconnect-core:1.6.5.200'
}

5.同步工程配置

布局设计
参考如下设置布局,添加一个“CustomAttributes”按钮和result文本框。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp">
<Button
android:layout_marginTop="20dp"
android:id="@+id/CustomAttributes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CustomAttributes " />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dp"
android:textAlignment="center" />
</LinearLayout>

功能实现
在项目的MainActivity.java文件中导入头文件并定义相关界面元素。
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.huawei.agconnect.remoteconfig.AGConnectConfig;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button setCustomAttributes = findViewById(R.id.CustomAttributes);
textView = findViewById(R.id.result);
setCustomAttributes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setCustomAttributes();
}
});
}

通过setCustomAttributes(Map<String, String> map)方法设置需要传入的的自定义属性map1,并且通过getCustomAttributes()方法来获取云端的自定义属性map2并展示在界面。
public void setCustomAttributes(){
    Map<String, String> map1 = new HashMap<String, String>();
    map1.put("test1", "123");
    map1.put("test2", "456");
    map1.put("test3", "789");
    AGConnectConfig config = AGConnectConfig.getInstance();
    config.setCustomAttributes(map1);
    String map2 = config.getCustomAttributes().toString();
    textView.setText(map2);
    Log.i(TAG,map2);
}
功能测试
在Android Studio上运行项目安装APK包,点击“CustomAttributes”按钮,设置自定义属性,并且从云端获取传入的自定义属性,展示在界面上。

结论
经过测试,通过setCustomAttributes(Map<String, String> map)和getCustomAttributes()方法,可以正常在端侧传入自定义属性,获取云端配置数据。
欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh










