HarmonyOS 退出应用程序代码?

麻烦提供一个退出应用程序的代码吧,类似崩溃直接退出应用

阅读 404
1 个回答

参考demo:

import { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  context = getContext(this) as common.UIAbilityContext
  build() {

    Button("appCrash").onClick(()=>{
      try {
        this.context.terminateSelf((err: BusinessError) => {
          if (err.code) {
            // 处理业务逻辑错误
            console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`);
            return;
          }
          // 执行正常业务
          console.info('terminateSelf succeed');
        });
      } catch (err) {
        // 捕获同步的参数错误
        let code = (err as BusinessError).code;
        let message = (err as BusinessError).message;
        console.error(`terminateSelf failed, code is ${code}, message is ${message}`);
      }
    })
  }
}
logo
HarmonyOS
子站问答
访问
宣传栏