Flutter OHOS fluttertpc_app_installer(打开应用商店和安装 APP)
 作者:flfljh
- 2024-12-19  湖南
- 本文字数:1228 字 - 阅读完需:约 4 分钟 
fluttertpc_app_installer
打开应用商店和安装 APP
用法
String androidAppId = '';
String iOSAppId = '';
String ohosAppId = '';
AppInstaller.goStore(androidAppId, iOSAppId, ohosAppId);
AppInstaller.installApk('/sdcard/apk/app-debug.apk');
鸿蒙 OS 代码
获取 app 包名
     getAppPackageName(appId: String): String {let appPackageName: String = appId;if (appPackageName == null || appPackageName == '') {  bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION).then(bundleInfo => {appPackageName = bundleInfo.name;console.info("AppInstallerOhosPlugin appPackageName is then  " + appPackageName);  });}return appPackageName;  }
复制代码
 打开市场
    /**   * open appstore   * */  goAppStore(appId: String, result: MethodResult) {if (appId == null) {  console.info("AppInstallerOhosPlugin appPackageName is null  ");  return;}try {  const request: Want = {parameters: {  bundleName: appId}  };  productViewManager.loadProduct(getContext(this) as common.UIAbilityContext, request, {onError: (error: BusinessError) => {  console.info('AppInstallerOhosPlugin',`loadProduct onError.code is ${error.code}, message is ${error.message}`);  result.success(false);}  });  result.success(true);} catch (err) {  console.info('AppInstallerOhosPlugin', `loadProduct failed.code is ${err.code}, message is ${err.message}`);  result.success(false);}  }
复制代码
 安装 app
  installApp(appSrc: String) {//1.get permissionstry {  let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();  atManager.requestPermissionsFromUser(getContext(this), ['ohos.permission.ENTERPRISE_INSTALL_BUNDLE']).then(async (permission) => {  // if (permission.authResults[0] !== 0) {  //   return  // }  this.startInstallApp(appSrc);});} catch (err) {  console.info('AppInstallerOhosPlugin',`Permission application failed.code is ${err.code}, message is ${err.message}`);}  }
  startInstallApp(appSrc: String) {//2.to installlet bundleId: String = this.getAppPackageName('');let wantTemp: Want = {  bundleName: bundleId.toString(),  abilityName: 'EntryAbility',};let hapFilePaths: Array<string> = [appSrc?.toString()];
installBundle.install(wantTemp, hapFilePaths).then(() => {  console.info('AppInstallerOhosPlugin Succeeded in installing bundles.');}).catch((err: BusinessError) => {  console.error(`AppInstallerOhosPlugin Failed to install bundles. Code is ${err.code}, message is ${err.message}`);});  }
复制代码
 划线
评论
复制
发布于: 13 分钟前阅读数: 5

flfljh
关注
还未添加个人签名 2024-10-29 加入
还未添加个人简介







 
    
 
				 
				 
			


评论