HarmonyOS鸿蒙Next中远程配置如何传入自定义属性

现在HarmonyOS鸿蒙Next中远程配置端侧服务提供的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/' }
    }
}
  1. 在应用级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'
}
  1. 同步工程配置

布局设计

参考如下设置布局,添加一个“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();
            }
        });
    }

    public void setCustomAttributes(){
        Map<String, String> map1 = new HashMap<>();
        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);
    }
}

通过setCustomAttributes(Map<String, String> map)方法设置需要传入的的自定义属性map1,并且通过getCustomAttributes()方法来获取云端的自定义属性map2并展示在界面。

功能测试

在Android Studio上运行项目安装APK包,点击“CustomAttributes”按钮,设置自定义属性,并且从云端获取传入的自定义属性,展示在界面上。

结论

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

7 回复

学习一下

更多关于HarmonyOS鸿蒙Next中远程配置如何传入自定义属性的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:BV1S4411E7LY/?p=17

在HarmonyOS鸿蒙Next中,远程配置可以通过@ohos.cloud.remoteconfig模块实现。要传入自定义属性,首先在云端配置平台定义属性键值对,然后在应用中使用remoteconfig.getConfig()方法获取配置。自定义属性可以通过remoteconfig.applyConfig()方法应用到应用中。确保在config.json中声明ohos.permission.CLOUDRC权限,并在应用启动时调用remoteconfig.init()初始化远程配置服务。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!