鸿蒙 应用市场更新功能:版本检测与更新提醒

在应用开发中,版本更新是一个常见需求。应用市场更新功能为已上架应用提供版本检测、显示更新提醒能力,可以在应用内提醒用户及时更新到最新版本。

一、场景

当应用启动完成或用户在应用中主动检查应用新版本时,可以通过本服务来查询应用是否有可更新的版本。如果存在可更新版本,可以通过本服务为用户显示更新提醒。

二、版本支持

限制项 说明
设备支持 Phone、Tablet、PC/2in1、TV(19+)、Wearable(20+)
模拟器 不支持(请使用真机调试,模拟器会提示"无法获取内容,请点击屏幕重试")
应用上架 应用必须在应用市场已上架

三、核心接口

接口 描述
checkAppUpdate(context) 检查更新接口,用于检测当前是否有新版本,返回Promise<CheckUpdateResult>
showUpdateDialog(context) 显示升级对话框接口,用于提示用户进行升级,返回Promise<ShowUpdateResultCode>

四、开发步骤

4.1 检测应用新版本

步骤1:导入模块
复制代码
import { updateManager } from '@kit.AppGalleryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import type { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
步骤2:构造Context参数
复制代码
let context: common.UIAbilityContext = 
    this.getUIContext().getHostContext() as common.UIAbilityContext;
步骤3:调用checkAppUpdate检查更新
复制代码
try {
    updateManager.checkAppUpdate(context)
        .then((checkResult: updateManager.CheckUpdateResult) => {
            hilog.info(0, 'TAG', "Succeeded in checking Result updateAvailable:" + checkResult.updateAvailable);
        })
        .catch((error: BusinessError) => {
            hilog.error(0, 'TAG', `checkAppUpdate error: ${error.code}, message: ${error.message}`);
        });
} catch (error) {
    hilog.error(0, 'TAG', `checkAppUpdate error: ${error.code}, message: ${error.message}`);
}

检测条件说明

条件 要求
版本要求 本地安装版本须低于应用市场在架版本
签名要求 本地安装版本须和应用市场在架版本签名信息保持一致
测试类型 暂不支持邀请测试和公开测试

4.2 显示升级对话框

步骤1:导入模块
复制代码
import { updateManager } from '@kit.AppGalleryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import type { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
步骤2:构造Context参数
复制代码
let context: common.UIAbilityContext = 
    this.getUIContext().getHostContext() as common.UIAbilityContext;
步骤3:调用showUpdateDialog显示升级对话框
复制代码
try {
    updateManager.showUpdateDialog(context)
        .then((resultCode: updateManager.ShowUpdateResultCode) => {
            hilog.info(0, 'TAG', "Succeeded in showing UpdateDialog resultCode:" + resultCode);
        })
        .catch((error: BusinessError) => {
            hilog.error(0, 'TAG', `showUpdateDialog error: ${error.code}, message: ${error.message}`);
        });
} catch (error) {
    hilog.error(0, 'TAG', `showUpdateDialog error: ${error.code}, message: ${error.message}`);
}

五、完整代码

复制代码
import { updateManager } from '@kit.AppGalleryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import type { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
    private context: common.UIAbilityContext = 
        this.getUIContext().getHostContext() as common.UIAbilityContext;

    build() {
        Column() {
            Button('检查更新')
                .onClick(() => {
                    this.checkUpdate();
                })
                .margin(10)
            
            Button('显示升级对话框')
                .onClick(() => {
                    this.showUpdateDialog();
                })
                .margin(10)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
    }

    // 检查应用新版本
    private checkUpdate(): void {
        try {
            updateManager.checkAppUpdate(this.context)
                .then((checkResult: updateManager.CheckUpdateResult) => {
                    if (checkResult.updateAvailable) {
                        hilog.info(0, 'TAG', '有新版本可用');
                        // 有新版本,可以调用showUpdateDialog显示更新提醒
                        this.showUpdateDialog();
                    } else {
                        hilog.info(0, 'TAG', '当前已是最新版本');
                        this.showToast('当前已是最新版本');
                    }
                })
                .catch((error: BusinessError) => {
                    hilog.error(0, 'TAG', `checkAppUpdate error: ${error.code}, ${error.message}`);
                    this.showToast('检查更新失败');
                });
        } catch (error) {
            hilog.error(0, 'TAG', `checkAppUpdate error: ${error.code}, ${error.message}`);
            this.showToast('检查更新失败');
        }
    }

    // 显示升级对话框
    private showUpdateDialog(): void {
        try {
            updateManager.showUpdateDialog(this.context)
                .then((resultCode: updateManager.ShowUpdateResultCode) => {
                    hilog.info(0, 'TAG', `showUpdateDialog resultCode: ${resultCode}`);
                })
                .catch((error: BusinessError) => {
                    hilog.error(0, 'TAG', `showUpdateDialog error: ${error.code}, ${error.message}`);
                    this.showToast('显示更新提醒失败');
                });
        } catch (error) {
            hilog.error(0, 'TAG', `showUpdateDialog error: ${error.code}, ${error.message}`);
            this.showToast('显示更新提醒失败');
        }
    }

    private showToast(msg: string) {
        this.getUIContext().getPromptAction().showToast({
            message: msg,
            duration: 2000
        });
    }
}
相关推荐
前端不太难7 小时前
从单页面到系统化:鸿蒙 App 演进路径
华为·状态模式·harmonyos
想你依然心痛9 小时前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“文思智脑“——PC端AI智能体沉浸式智能写作工作台
人工智能·ar·harmonyos·ai写作
小雨青年9 小时前
鸿蒙 HarmonyOS 6 | Pura X Max 鸿蒙原生适配 09:展开态列表增加字段但不变复杂
华为·harmonyos
richard_yuu9 小时前
鸿蒙治愈游戏模块实战|四大轻量解压游戏、ArkTS动画交互与低功耗落地
游戏·交互·harmonyos
阿钱真强道13 小时前
24 鸿蒙LiteOS GPIO中断实战:从原理到上升沿/下降沿详解
harmonyos·中断·rk·liteos·开源鸿蒙·瑞芯微·rk2206
cd_9492172115 小时前
鸿蒙系统下抖音存储空间不足怎么办?缓存清理教程
缓存·华为·harmonyos
轻口味18 小时前
HarmonyOS 6.1 全栈实战录 - 14 渲染树透镜:FrameNode 渲染状态感知与高性能 UI 调优实战
ui·华为·harmonyos
HwJack2018 小时前
HarmonyOS NEXT 游戏APP开发中如何正确拦截退出手势
游戏·华为·harmonyos
HwJack2018 小时前
HarmonyOS APP开发中ArkTS/JS 类型错误全景拆解
javascript·华为·harmonyos
lqj_本人19 小时前
鸿蒙PC:鸿蒙版本 Electron 框架环境搭建并且实现 XH 笔记应用
笔记·electron·harmonyos