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

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

阅读 433
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;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进