首页 > 其他分享 >【AGC】远程配置如何传入自定义属性

【AGC】远程配置如何传入自定义属性

时间:2022-11-15 09:59:17浏览次数:64  
标签:自定义 setCustomAttributes AGC huawei import com 远程 agconnect

背景:

现在AGC远程配置端侧服务提供的SDK支持传入自定义属性获取和更新云端配置数据了。下面将通过一个demo集成远程配置SDK来实现这一功能。

 

集成准备

1.在AGC创建工程并开通远程配置服务。

2.在Android Studio中创建一个工程,将agconnect-services.json文件拷贝到项目的app目录下。

cke_485.png

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.同步工程配置

cke_2407.png

 

布局设计

参考如下设置布局,添加一个“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”按钮,设置自定义属性,并且从云端获取传入的自定义属性,展示在界面上。

cke_9365.png

结论

经过测试,通过setCustomAttributes(Map<String, String> map)和getCustomAttributes()方法,可以正常在端侧传入自定义属性,获取云端配置数据。

欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

标签:自定义,setCustomAttributes,AGC,huawei,import,com,远程,agconnect
From: https://www.cnblogs.com/developer-huawei/p/16891396.html

相关文章

  • 1710. 卡车上的最大单元数 ----- 贪心算法,自定义sort排序
    请你将一些箱子装在一辆卡车上。给你一个二维数组boxTypes,其中boxTypes[i]=[numberOfBoxesi,numberOfUnitsPerBoxi]:numberOfBoxesi是类型i的箱子的数量。numb......
  • JavaScript自定义数据类型判断函数
    functionjudgeType(ele){letres=typeofele;if(res==="object"){//短路表达式,第一个成立则返回第二个的值,第一个不成立,则返回第一个的值......
  • 自定义组件
    创建自定义组件类似于页面,一个自定义组件由jsonwxmlwxssjs4个文件组成。要编写一个自定义组件,首先需要在json文件中进行自定义组件声明(将component字段设为true......
  • 自定义页面
    [Page构造器]Page({/***页面的初始数据*/data:{},/***生命周期函数--监听页面加载*/onLoad:function(e){},/**......
  • 自定义组件传参
    参数声明Component({//参数properties:{//这里定义了innerText属性,属性值可以在组件使用时指定innerText:{type:String,value:'def......
  • 自定义组件事件
    子组件<viewbindtap="onItemChildClick"data-item="item">{{item}}</view>Component({methods:{onItemChildClick(event){this.trigger......
  • 第四十七章 开发自定义标签 - 服务器文档对象模型
    第四十七章开发自定义标签-服务器文档对象模型当CSP编译器处理CSP文档时,它首先查找与规则匹配相关的所有标记。当编译器扫描文档时,它会创建一个与CSP文档中包含的标记......
  • mysql 配置远程访问
    先使用root用户进入mysqlmysql-uroot-p创建新用户createuser用户名identifiedby'密码';赋予所有权限GRANTALLPRIVILEGESON*.*TO'用户名'@'%'IDENTIFI......
  • 4K60帧!RayLink远程控制软件如何帮助设计师远程办公?
    司机在家能远程工作吗?大写的“能”!!!如果你听说过司机远程办公,那你一定对下面这张图不陌生~(远程办公的叉车司机)这张图展示的是负责在库房工作的叉车司机在非办公区域远程办......
  • Android Service学习之AIDL, Parcelable和远程服务
    AIDL的作用由于每个应用程序都运行在自己的进程空间,并且可以从应用程序UI运行另一个服务进程,而且经常会在不同的进程间传递对象。在Android平台,一个进程通常不能访问另......