OpenHarmony开发者论坛
标题:
设备端直装hap的具体方案1:使用包管理API
[打印本页]
作者:
westinyang
时间:
2023-10-27 10:07
标题:
设备端直装hap的具体方案1:使用包管理API
[md]> 关于作者:[I'm westinyang](
https://kaihongpai.feishu.cn/wiki/CqWLwJRadibxztkrIWZcogWxnXd
)
# 序言
之前在《OpenHarmony设备直装hap脱离电脑hdc的两种实现思路》一文中,我提到了两种实现直装hap的思路,那本篇文章我将详细讲解下第一种思路的具体实现方式
# 项目权限配置
entry/src/main/module.json5
```json
{
"name": "ohos.permission.INSTALL_BUNDLE",
},
```
# 访问控制权限配置
openharmony-sdk\9\toolchains\lib\UnsgnedReleasedProfileTemplate.json
```json
"apl":"system_core",
"app-feature":"hos_system_app"
```
# 安装应用
- hap包要放到设备真实路径:`/data/app/el2/用户ID/base/应用包名/haps/entry/files/`
- hapFilePaths写沙箱路径:`/data/storage/el2/base/haps/entry/files/`
```js
import installer from '@ohos.bundle.installer';
import promptAction from '@ohos.promptAction';
function installApp() {
let hapFilePaths = ['/data/storage/el2/base/haps/entry/files/testapp.hap'];
let installParam = {
userId: 100,
isKeepData: false,
installFlag: 1,
};
try {
installer.getBundleInstaller().then(data => {
data.install(hapFilePaths, installParam, err => {
if (err) {
console.error('install failed:' + err.message);
promptAction.showToast({message: '安装失败:' + err.message, duration: ToastDuration});
} else {
console.info('install successfully.');
promptAction.showToast({message: '安装成功', duration: ToastDuration});
}
});
}).catch(error => {
console.error('getBundleInstaller failed. Cause: ' + error.message);
promptAction.showToast({message: '安装失败:' + error.message, duration: ToastDuration});
});
} catch (error) {
console.error('getBundleInstaller failed. Cause: ' + error.message);
promptAction.showToast({message: '安装失败:' + error.message, duration: ToastDuration});
}
}
```
# 打开应用
指定包名和abilityName,调用context.startAbility打开目标应用
```js
import common from '@ohos.app.ability.common';
import promptAction from '@ohos.promptAction';
function openApp() {
let context = getContext(this) as common.UIAbilityContext;
context.startAbility({
bundleName: 'com.example.testapp',
abilityName: 'EntryAbility',
moduleName: ''
}).then(() => {
console.error('startApplication promise success');
promptAction.showToast({message: '打开成功', duration: ToastDuration});
}, (err) => {
console.error(`startApplication promise error: ${JSON.stringify(err)}`);
promptAction.showToast({message: '打开失败:应用不存在', duration: ToastDuration});
});
}
```
# 卸载应用
指定包名和参数卸载应用
```js
import installer from '@ohos.bundle.installer';
import promptAction from '@ohos.promptAction';
function uninstallApp() {
let bundleName = 'com.example.testapp';
let installParam = {
userId: 100,
isKeepData: false,
installFlag: 1
};
try {
installer.getBundleInstaller().then(data => {
data.uninstall(bundleName, installParam, err => {
if (err) {
console.error('uninstall failed:' + err.message);
promptAction.showToast({message: '卸载失败:应用不存在', duration: ToastDuration});
} else {
console.info('uninstall successfully.');
promptAction.showToast({message: '卸载成功', duration: ToastDuration});
}
});
}).catch(error => {
console.error('getBundleInstaller failed. Cause: ' + error.message);
promptAction.showToast({message: '卸载失败:' + error.message, duration: ToastDuration});
});
} catch (error) {
console.error('getBundleInstaller failed. Cause: ' + error.message);
promptAction.showToast({message: '卸载失败:' + error.message, duration: ToastDuration});
}
}
```
# 持续关注
- 关于作者:[I'm westinyang](
https://kaihongpai.feishu.cn/wiki/CqWLwJRadibxztkrIWZcogWxnXd
)
- 哔哩哔哩:[个人主页](
https://space.bilibili.com/74433635
)
[/md]
欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/)
Powered by Discuz! X3.5