HarmonyOS 鸿蒙Next中实现应用包更新完整代码如下:
配置版本号
AppScope/app.json5
{
"app": {
"bundleName": "com.example.harmonyos35",
"vendor": "example",
"versionCode": 1000000,
"versionName": "1.0.0",
"icon": "$media:app_icon",
"label": "$string:app_name"
}
}
bundleManager获取版本号
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-bundlemanager-V5
import bundleManager from '@ohos.bundle.bundleManager';
let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT;
try {
bundleManager.getBundleInfoForSelf(bundleFlags).then((data) => {
this.message='getBundleInfoForSelf'+JSON.stringify(data)
}).catch(err => {
hilog.error(0x0000, 'testTag', 'getBundleInfoForSelf failed. Cause: %{public}s', err.message);
});
} catch (err) {
hilog.error(0x0000, 'testTag', 'getBundleInfoForSelf failed: %{public}s', err.message);
}
跳转到应用市场执行更新
其中 com.taobao.taobao4hmos 需要改成自己应用的包名称
goToMarket(){
let context = getContext(this) as common.UIAbilityContext;
let bundleName="com.taobao.taobao4hmos"; //应用包名称
let want: Want = {
action: 'ohos.want.action.appdetail',
uri: 'store://appgallery.huawei.com/app/detail?id=' + bundleName, // bundleName为需要打开应用详情的应用的包名
};
context.startAbility(want).then(() => {
console.info('Start Ability successfully.');
}).catch((err: BusinessError) => {
console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
});
}
Api接口
get方式请求,appVersion为服务器版本号,方便测试服务器版本号可以url传入
https://xiaomi.itying.com/api/getAppVersion?versionCode=1000002