中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
如何获取应用信息以及彻底退出APP
微信扫码分享
import { bundleManager } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { DateUtil } from '../../utils/DateUtil'; import { common } from '@kit.AbilityKit'; @Entry @Component struct CloseApp { @State message: string = '应用信息: ' @State bundleInfo: bundleManager.BundleInfo | null = null private context = getContext(this) as common.UIAbilityContext; aboutToAppear(): void { this.getAppPackageInfo() } /* 获取应用包信息 */ getAppPackageInfo() { let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION; try { this.bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleFlags); console.log('bundle-info', 'getBundleInfoForSelfSync successfully: %{public}s', JSON.stringify(this.bundleInfo)) } catch (err) { let message = (err as BusinessError).message; console.log('bundle-info', 'getBundleInfoForSelfSync failed: %{public}s', message); } } closeApp() { this.context.terminateSelf() } build() { Scroll() { Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Start }) { Text(this.message) .id('System') .fontSize(20) .fontWeight(FontWeight.Bold) .margin({ bottom: 5 }) .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) if (this.bundleInfo) { Column() { Row() { Text('应用包名称: ') Text(this.bundleInfo.name) }.width('100%') Row() { Text('应用包供应商: ') Text(this.bundleInfo.vendor) }.width('100%') Row() { Text('应用包版本号: ') Text(this.bundleInfo.versionCode.toString()) }.width('100%') Row() { Text('应用包版本名称: ') Text(this.bundleInfo.versionName) }.width('100%') Row() { Text('应用运行目标版本: ') Text(this.bundleInfo.targetVersion.toString()) }.width('100%') Row() { Text('应用运行目标版本: ') Text(this.bundleInfo.targetVersion.toString()) }.width('100%') Row() { Text('应用包安装时间: ') Text(`${this.bundleInfo.installTime.toString()} -- ${DateUtil.getFormatTimeStr(this.bundleInfo.installTime)}`) .fontSize(14) }.width('100%') Row() { Text('应用包更新时间: ') Text(`${this.bundleInfo.updateTime.toString()} -- ${DateUtil.getFormatTimeStr(this.bundleInfo.updateTime)}`) .fontSize(14) }.width('100%') } } Button('退出应用').width('100%').margin({ top: 10 }).onClick(() => { this.closeApp() }) } .height('100%') .width('100%') } .padding(10) .width('100%') .height('100%') } }