如何获取应用信息以及彻底退出APP

如何获取应用信息以及彻底退出APP

HarmonyOS
2024-07-21 19:07:25
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
anlan001
  • 对于应用自身信息的获取方式,可以通过bundleManager获取。bundleManager可以获取Ability信息、ApplicationInfo应用程序信息、BundleInfo应用包信息等
  • 彻底退出APP应该走UIAbility的正常生命周期。而ApplicationContext下的killAllProcesses是找出应用下面所有的进程,逐个结束掉。对于用户不同意隐私协议,需要退出的场景,推荐使用UIAbilityContext下的terminateSelf。
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%') 
  } 
}
分享
微博
QQ
微信
回复
2024-07-22 11:08:58
相关问题
如何获取应用信息以及彻底退出APP
164浏览 • 1回复 待解决
线程信息以及线程的任务栈如何获取
1284浏览 • 1回复 待解决
如何获取应用版本信息
1817浏览 • 1回复 待解决
如何获取app配置的版本信息
1431浏览 • 1回复 待解决
app.getInfo获取应用配置信息为空。
421浏览 • 1回复 待解决
应用如何获取应用的metadata信息
530浏览 • 1回复 待解决
如何在Native侧获取APP版本信息
1889浏览 • 1回复 待解决
arkTS获取app版本信息的方法。
267浏览 • 1回复 待解决
如何获取App版本号,版本名等信息
2896浏览 • 1回复 待解决
有谁知道如何获取应用信息
1467浏览 • 1回复 待解决
三方应用如何获取http代理信息
1366浏览 • 1回复 待解决
harmony获取其他应用信息
527浏览 • 0回复 待解决
有谁知道如何强制退出app?
137浏览 • 1回复 待解决
如何退出应用,你了解吗?
1829浏览 • 1回复 待解决
工信部ICP APP备案 HarmonyOSAPP信息获取
635浏览 • 1回复 待解决
如何获取获取OAID信息
114浏览 • 1回复 待解决
应用通过什么接口获取设备标识信息
1345浏览 • 1回复 待解决
stage模型怎么获取app的版本号信息
1484浏览 • 1回复 待解决