HarmonyOS 如何监听应用关闭事件?

应用关闭之前需要加确认关闭防呆 如何实现呢

阅读 440
1 个回答

请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-app-ability-uiability-V5\#uiabilityonpreparetoterminate10

配置权限,

'requestPermissions': [{
  'name': 'ohos.permission.PREPARE_APP_TERMINATE'
}],
onPrepareToTerminate(): boolean {
  console.info('onBackPress:');
  promptAction.showDialog({
    title: "提示",
    message: "确认退出?",
    buttons: [
      {
        text: "取消",
        color: "#000000"
      },
      {
        text: "退出",
        color: "#555555"
      }
    ]
  }).then((data: ShowDialogSuccessResponse) => {
    console.info('showDialog success, click button: ' + data.index);
    if (data.index == 1) {
      this.context.terminateSelf()
    }
  })
  return true;
}