[经验分享] 设备端直装hap的具体方案1:使用包管理API 原创

westinyang 显示全部楼层 发表于 2023-10-27 10:07:22

关于作者:I'm westinyang

序言

之前在《OpenHarmony设备直装hap脱离电脑hdc的两种实现思路》一文中,我提到了两种实现直装hap的思路,那本篇文章我将详细讲解下第一种思路的具体实现方式

项目权限配置

entry/src/main/module.json5

{
  "name": "ohos.permission.INSTALL_BUNDLE",
},

访问控制权限配置

openharmony-sdk\9\toolchains\lib\UnsgnedReleasedProfileTemplate.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/
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打开目标应用

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});
  });
}

卸载应用

指定包名和参数卸载应用

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});
  }
}

持续关注

©著作权归作者所有,转载或内容合作请联系作者

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部