【Codelabs挑战赛—定制消息和配置】HarmonyOS定制告白消息

原创
05/30 16:11
阅读数 2.4K

1.效果如下所示:

 

点击查看

2.项目介绍:

开发环境:DevEco Studio 3.0 Beta2 3.0.0.800  Empty Ability Template  Java

通过此项目,主要是学习如何在HarmonyOS上集成远程配置、应用内消息、AppLinking链接、云存储和分析服务等技术。

3.项目结构:

%E6%88%AA%E5%9B%BE.PNG

4.在Gradle文件中设置AppGallery Connect的相关依赖和SDK包:

a) 设置项目级 build.gradle:

apply plugin: 'com.huawei.ohos.app'
	
buildscript {
    repositories {
        maven {
            url 'https://repo.huaweicloud.com/repository/maven/'
        }
        maven {
            url 'https://developer.huawei.com/repo/'
        }
    }
    ...
    dependencies {
        classpath 'com.huawei.ohos:hap:3.0.5.2'
        classpath 'com.huawei.ohos:decctest:1.2.7.2'
        // 添加agconnect服务依赖
        classpath 'com.huawei.agconnect:agcp-harmony:1.3.0.300'
    }
}

allprojects {
    repositories {
        maven {
            url 'https://repo.huaweicloud.com/repository/maven/'
        }
        maven {
            url 'https://developer.huawei.com/repo/'
        }
    }
}

b) 设置模块级 build.gradle:

// Add the information to the bottom of the file.
apply plugin: 'com.huawei.ohos.hap'
apply plugin: 'com.huawei.agconnect'
apply plugin: 'com.huawei.ohos.decctest'
...
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
    testImplementation 'junit:junit:4.13.1'
    ohosTestImplementation 'com.huawei.ohos.testkit:runner:2.0.0.200'

    // 添加分析服务依赖
    implementation 'com.huawei.hms:hianalytics-harmony:6.3.2.301'

    // AGC基础SDK
    implementation 'com.huawei.agconnect:agconnect-core-harmony:1.3.0.300'

    // 调测设备AAID SDK
    implementation 'com.huawei.agconnect:agconnect-credential-harmony:1.3.0.300'

    // 集成应用内消息SDK
    implementation 'com.huawei.agconnect:agconnect-appmessaging-harmony:1.3.0.300'

    // 添加远程配置依赖
    implementation 'com.huawei.agconnect:agconnect-remoteconfig-harmony:1.3.0.300'

    // 添加AppLinking SDK
    implementation 'com.huawei.agconnect:agconnect-applinking-harmony:1.3.0.300'
}

5.云存储:

4.PNG

6.远程配置:

%E8%BF%9C%E7%A8%8B%E9%85%8D%E7%BD%AE.PNG

7.应用内消息:

%E5%BA%94%E7%94%A8%E5%86%85%E6%B6%88%E6%81%AF.PNG

8.App Linking:

applinking.PNG

1.PNG

2.PNG

3.PNG

9.添加配置文件:

在AGC管理平台打开“项目设置”,下载配置文件“agconnect-services.json”,将下载的JSON文件放到DevEco Studio 项目的应用级目录下:

66.PNG

10.分析服务:

由于应用集成了华为分析服务,完成相关配置后,打开应用,会弹出定制消息,此时会上报应用信息到AGC管理平台。

13.PNG

14.PNG

11.程序代码:

ability_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="top"
    ohos:orientation="vertical">

    <Image
        ohos:id="$+id:image"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:image_src="$media:first" />

</DirectionalLayout>

MainAbility:

package com.example.codechallengesdemo;

import com.example.codechallengesdemo.slice.MainAbilitySlice;
import com.huawei.agconnect.AGConnectInstance;
import com.huawei.hms.analytics.HiAnalytics;
import com.huawei.hms.analytics.HiAnalyticsInstance;
import com.huawei.hms.analytics.HiAnalyticsTools;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        // 初始化AGC SDK
        AGConnectInstance.initialize(getAbilityPackage());
        // 打开SDK日志开关
        HiAnalyticsTools.enableLog();
        // 初始化hiAnalytics kit
        HiAnalyticsInstance instance = HiAnalytics.getInstance(this);
    }
}

MainAbilitySlice:

package com.example.codechallengesdemo.slice;

import com.example.codechallengesdemo.ResourceTable;
import com.huawei.agconnect.appmessaging.AGConnectAppMessaging;
import com.huawei.agconnect.appmessaging.AGConnectAppMessagingOnClickListener;
import com.huawei.agconnect.appmessaging.model.Action;
import com.huawei.agconnect.appmessaging.model.AppMessage;
import com.huawei.agconnect.common.api.AGCInstanceID;
import com.huawei.agconnect.remoteconfig.AGConnectConfig;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.audio.*;
import ohos.media.codec.Codec;
import ohos.media.common.BufferInfo;
import ohos.media.common.Source;

import java.nio.ByteBuffer;

public class MainAbilitySlice extends AbilitySlice {
    private AGConnectAppMessaging appMessaging;

    private static final String TAG = MainAbilitySlice.class.getName();

    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG);

    private static final int SAMPLE_RATE = 44100;

    private static final int BUFFER_SIZE = 1024;

    private AudioRenderer audioRenderer;

    private Codec codec;

    long interval = 0;

    private String flag = "late";
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 初始化音频播放相关参数
        initAudioRenderer();

        // 获取AAID
        getAAID();

        //配置应用内消息
        configAppMessage();

        // 读取远程配置
        readRemoteConfig();
    }

安装应用后,打开应用程序,会播放音乐,更换应用内图片,同时弹出定制消息,点击按钮跳转到远程配置的链接。至此,HarmonyOS定制告白消息项目就结束了。

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部