HarmonyOS鸿蒙Next中跳转其他应用闪退

HarmonyOS鸿蒙Next中跳转其他应用闪退

import { BusinessError } from '@kit.BasicServicesKit';
import { bundleManager, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

class SchemeModel {
  appName: string = '';
  scheme: string = '';
  domain?: string = '';
  path?: string = '';

  constructor(appName: string, scheme: string, domain: string = 'home', path: string = '') {
    this.appName = appName;
    this.scheme = scheme;
    this.domain = domain;
    this.path = path;
  }

  getDeepLinking() {
    return this.path ? `${this.scheme}://${this.domain}/${this.path}` : `${this.scheme}://${this.domain}`;
  }
}

@Entry
@Component
struct ddd {
  context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  promptAction = this.getUIContext().getPromptAction();
  @State curIndex: number = 0;
  schemeModels: SchemeModel[] = [
    new SchemeModel('华为地图(花瓣地图)', 'maps'),
    new SchemeModel('抖音', 'snssdk1128'),
    new SchemeModel('微博', 'sinaweibo'),
    new SchemeModel('小红书', 'xhsdiscover'),
    new SchemeModel('哔哩哔哩', 'bilibili'),
    new SchemeModel('快手', 'kwai'),
    new SchemeModel('微信', 'weixin'),
    new SchemeModel('QQ', 'mqqapi'),
    new SchemeModel('支付宝', 'alipays'),
    new SchemeModel('云闪付', 'upwallet'),
    new SchemeModel('京东', 'openapp.jdmobile'),
    // 美团的Deep Linking必须加上美团域名才能拉起
    new SchemeModel('美团', 'imeituan', 'www.meituan.com'),
    new SchemeModel('拼多多', 'pinduoduo'),
    // 淘宝的Deep Linking必须加上淘宝域名和路径才能拉起
    new SchemeModel('淘宝', 'tbopen', 'm.taobao.com/tbopen/index.html'),
    new SchemeModel('高德地图', 'amapuri'),
    new SchemeModel('腾讯会议', 'wemeet'),
    new SchemeModel('知乎', 'zhihu')
  ];
  appNames: SelectOption[] = [];

  aboutToAppear(): void {
    this.appNames = this.schemeModels.flatMap(schemeModel => {
      return { value: schemeModel.appName } as SelectOption;
    });
  }

  build() {
    Column({ space: 20 }) {
      Select(this.appNames)
        .selected(this.curIndex)
        .value(this.schemeModels[this.curIndex].appName)
        .font({ size: 16, weight: 500 })
        .optionWidth(200)
        .optionHeight(300)
        .onSelect((index: number, text?: string | undefined) => {
          hilog.info(0x0000, 'Testlog', `Select index: ${index}, text: ${text}`);
          this.curIndex = index;
        })
        .avoidance(AvoidanceMode.AVOID_AROUND_TARGET);

      Button('跳转')
        .width(200)
        .height(50)
        .onClick(() => {
          let deepLink: string = this.schemeModels[this.curIndex].getDeepLinking();
          hilog.info(0x0000, 'Testlog', `deepLink: ${deepLink}`);
          try {
            // 使用canOpenLink判断是否可以访问
            let data = bundleManager.canOpenLink(deepLink);
            if (data) {
              // 使用openLink打开应用
              this.context.openLink(deepLink, { appLinkingOnly: false })
                .then(() => {
                  hilog.info(0x0000, 'Testlog', 'openlink success.');
                })
                .catch((error: BusinessError) => {
                  this.promptAction.showToast({ message: `打开失败: ${error.name}` });
                  hilog.error(0x0000, 'Testlog',
                    `openlink failed. error code:${error.code}, error message:${error.message}`);
                });
            } else {
              this.promptAction.showToast({ message: `暂无可用打开方式` });
            }
            hilog.info(0x0000, 'testTag', 'canOpenLink successfully: %{public}s', data);
          } catch (err) {
            let message = (err as BusinessError).message;
            this.promptAction.showToast({ message: `打开失败: ${message}` });
            hilog.error(0x0000, 'testTag', 'canOpenLink failed: %{public}s', message);
          }
        });
    }
    .justifyContent(FlexAlign.Center)
    .height('100%')
    .width('100%');
  }
}

cke_2076.png

我加上了为啥,运行时会闪退呢


更多关于HarmonyOS鸿蒙Next中跳转其他应用闪退的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在HarmonyOS Next中,应用跳转闪退通常由权限配置问题导致。需检查应用是否在module.json5配置文件中正确声明了跳转目标应用的bundleName和abilityName,并确保目标应用已安装且版本兼容。同时确认请求方应用已获取ohos.permission.START_ABILITIES_FROM_BACKGROUND权限。若使用隐式跳转,需验证action和entities参数与目标应用过滤器配置完全匹配。

更多关于HarmonyOS鸿蒙Next中跳转其他应用闪退的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


从代码和日志截图分析,跳转其他应用闪退可能由以下原因导致:

  1. 权限配置缺失:在module.json5配置文件中需要声明ohos.permission.START_ABILITIES_FROM_BACKGROUND权限,用于后台启动其他应用的能力。

  2. URI Scheme格式问题:部分应用的Scheme格式需要完整路径,比如淘宝的tbopen需要完整域名路径m.taobao.com/tbopen/index.html。检查SchemeModel中每个应用的URI格式是否符合目标应用的要求。

  3. 目标应用未安装canOpenLink返回true仅表示系统识别该Scheme,但若目标应用未安装,openLink仍可能失败。建议在调用前确认设备已安装目标应用。

  4. 异步调用异常处理openLink是异步操作,但错误捕获可能未覆盖所有异常情况。确保Promise的catch块能正确处理所有BusinessError。

建议按顺序排查:首先检查权限配置,然后验证各应用Scheme的准确性,最后确认目标应用安装状态。

回到顶部