Flutter OHOS fluttertpc_app_installer(打开应用商店和安装APP)
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 permissions
try {
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 install
let 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}`);
});
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!