采用这种自定义弹框,需要自己维护新版本的版本号、大小、更新详情描述信息。
如何实现应用升级自定义弹窗并监控点击事件,以及实现强制更新功能?
实现应用升级自定义弹窗,可保持应用内弹窗样式的统一,或监控“立即更新”按钮。
操作步骤:
采用这种自定义弹框,需要自己维护新版本的版本号、大小、更新详情描述信息。
目前系统提供的更新弹窗没有提供强制更新功能,用户可以通过场景一中的应用升级自定义弹窗的基础上实现强制更新。
操作步骤:
- import { common, Want } from '@kit.AbilityKit';
- import { productViewManager, updateManager } from '@kit.AppGalleryKit';
- import { BusinessError } from '@kit.BasicServicesKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
-
- @CustomDialog
- export struct BaseComHud {
- controller: CustomDialogController;
-
- build() {
- Column() {
- Text('有新版本更新,前往应用市场更新~')
- .fontSize(16)
- .margin({ top: 10, bottom: 10 });
- Row() {
- Button('立即更新')
- .onClick(() => {
- hilog.info(0, 'TAG', '用户选择了立即更新');
- try {
- updateManager.checkAppUpdate(this.getUIContext().getHostContext() as common.UIAbilityContext)
- .then((checkResult: updateManager.CheckUpdateResult) => {
- hilog.info(0, 'TAG', `Succeeded in checking Result updateAvailable: ${checkResult.updateAvailable}`);
- }).catch((error: BusinessError) => {
- hilog.error(0, 'TAG', `checkAppUpdate onError.code is ${error.code}, message is ${error.message}`);
- });
- } catch (error) {
- hilog.error(0, 'TAG', `checkAppUpdate onError.code is ${error.code}, message is ${error.message}`);
- }
-
- const wantParam: Want = {
- parameters: {
- // 必填,此处填入要加载的应用包名,例如:bundleName:'com.xxx.xx'
- bundleName: 'com.xxx.xx',
- }
- };
- const callback: productViewManager.ProductViewCallback = {
- onError: (error: BusinessError) => {
- hilog.error(0, 'TAG', `loadService onError.code is ${error.code}, message is ${error.message}`);
- },
- onAppear: () => {
- hilog.info(0, 'TAG', `loadProduct onAppear.`);
- },
- onDisappear: () => {
- hilog.info(0, 'TAG', `loadProduct onDisappear.`);
- }
- };
- // 调用接口,拉起应用详情页
- productViewManager.loadProduct(this.getUIContext().getHostContext() as common.UIAbilityContext, wantParam,
- callback);
- });
- }
- }
- .backgroundColor(Color.White)
- .borderRadius(10)
- .padding(20);
- }
- }
-
- @Entry
- @Component
- struct Index {
- dialogController: CustomDialogController = new CustomDialogController({
- // 此处直接指定自定义弹窗,不要重新new
- builder: BaseComHud(),
- alignment: DialogAlignment.Center,
- });
- dialogController1: CustomDialogController = new CustomDialogController({
- // 此处直接指定自定义弹窗,不要重新new
- builder: BaseComHud(),
- alignment: DialogAlignment.Center,
- onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
- // 参数说明
- // PRESS_BACK为点击三键back、左滑/右滑、键盘ESC退出。
- // TOUCH_OUTSIDE为点击遮障层退出。
- // CLOSE_BUTTON为点击关闭按钮退出。
- // SLIDE_DOWN为下拉关闭(适用于半模态转场)
- if (dismissDialogAction.reason === DismissReason.PRESS_BACK) {
- // ...
- // 可使用dismiss方法可关闭弹窗,dismissDialogAction.dismiss()
- }
- },
- });
-
- build() {
- Column() {
- Button('更新弹窗')
- .onClick(() => {
- this.dialogController.open(); // 打开弹窗
- });
-
- Button('强制更新弹窗')
- .onClick(() => {
- this.dialogController1.open(); // 打开弹窗
- });
- }
- .width('100%')
- .height('100%')
- .justifyContent(FlexAlign.SpaceEvenly);
- }
- }
合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
解决方案
精准高效的一站式服务支持,助力开发者商业成功