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

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

HarmonyOS
2024-07-21 19:07:25
1110浏览
收藏 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%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
分享
微博
QQ
微信
回复
2024-07-22 11:08:58
相关问题
如何获取应用信息以及彻底退出APP
1456浏览 • 1回复 待解决
HarmonyOS 如何彻底退出app
572浏览 • 1回复 待解决
HarmonyOS 如何彻底退出应用
942浏览 • 1回复 待解决
线程信息以及线程的任务栈如何获取
2478浏览 • 1回复 待解决
HarmonyOS 应用缓存清理不彻底
669浏览 • 1回复 待解决
如何获取app配置的版本信息
2587浏览 • 1回复 待解决
app.getInfo获取应用配置信息为空。
1345浏览 • 1回复 待解决
HarmonyOS 如何获取应用信息
918浏览 • 1回复 待解决
如何获取应用版本信息
2942浏览 • 1回复 待解决
如何在Native侧获取APP版本信息
3027浏览 • 1回复 待解决
HarmonyOS 如何主动退出app
502浏览 • 1回复 待解决
HarmonyOS 应用如何获取设备信息
961浏览 • 1回复 待解决
应用如何获取应用的metadata信息
1945浏览 • 1回复 待解决
HarmonyOS 如何获取应用的包信息
1092浏览 • 1回复 待解决
arkTS获取app版本信息的方法。
1808浏览 • 1回复 待解决
HarmonyOS 应用指纹信息获取
932浏览 • 1回复 待解决
如何获取App版本号,版本名等信息
5485浏览 • 1回复 待解决
HarmonyOS 获取应用备案信息
746浏览 • 1回复 待解决