系统级弹窗的事件无法通过API捕获,可以根据具体的应用场景,通过CustomDialogController类显示自定义弹窗,监控点击事件,然后根据点击事件做一个对应事件。
【解决方案】
比如实现应用升级自定义弹窗,监控“以后再说”或“立即更新”按钮。
操作步骤:
1.调用updateManager.checkAppUpdate接口,检查到有更新。
2.调用自定义的UI更新弹框CustomDialog。
3.用户点击自定义弹框里的“立即更新”按钮,调用productViewManager.loadProduct接口展示应用详情页,用户在应用详情页里更新应用。
4.开发者结合自己的业务来监控“以后再说”或“立即更新”按钮,代码中是通过hilog输出日志体现。
示例代码参考:
import { common, Want } from "@kit.AbilityKit";
import { productViewManager, updateManager } from "@kit.StoreKit";
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.huawei.xxx.xxx'
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);
});
Button('以后再说')
.onClick(() => {
hilog.info(0, 'TAG', '用户选择了以后再说');
this.controller.close(); // 关闭弹窗
});
}
}
.backgroundColor(Color.White)
.borderRadius(10)
.padding(20);
}
}
@Entry
@Component
struct Index {
dialogController: CustomDialogController = new CustomDialogController({
// 此处直接指定自定义弹窗,不要重新new
builder: BaseComHud(),
alignment: DialogAlignment.Center,
});
build() {
Column() {
Button('显示弹窗')
.onClick(() => {
this.dialogController.open(); // 打开弹窗
});
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center);
}
}