HarmonyOS 意图框架InsightIntentContext.startAbility报错?

参考代码:

export default class IntentExecutorImpl extends InsightIntentExecutor {
  onExecuteInUIAbilityForegroundMode(name: string, param: Record<string, Object>, pageLoader: window.WindowStage): insightIntent.ExecuteResult {
    let want: Want = {
      bundleName: 'xxx',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      uri: 'xxx'
    };
    //报错
    this.context.startAbility(want);
    let result: insightIntent.ExecuteResult = {
      code: 0
    };
    return result;
  }
}

报错如下:

Reason:Error
Error name:Error
Error message:Operation not supported.
Error code:
  Stacktrace:
    at startAbility (/usr1/hmos_for_system/src/increment/sourcecode/foundation/ability/ability_runtime/frameworks/js/napi/insight_intent_context/insight_intent_context.js:23:23)
at onExecuteInUIAbilityForegroundMode (entry/src/main/ets/entryability/InsightIntentExecutorImpl.ets:48:9)
阅读 630
1 个回答

参考demo:

export default class IntentExecutorImpl extends InsightIntentExecutor {
  onExecuteInUIAbilityForegroundMode(name: string, param: Record<string, Object>,
    pageLoader: window.WindowStage): insightIntent.ExecuteResult {
    let want: Want = {
      bundleName: 'xxx',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      uri: 'xxx'
    };

    try {
      const context = getContext(this) as common.UIAbilityContext;
      context.startAbility(want, (error) => {
        if (error) {
          hilog.error(0x0000, 'testTag', 'Start ability failed with %{public}s', JSON.stringify(error));
        } else {
          hilog.info(0x0000, 'testTag', '%{public}s', 'Start ability succeed');
        }
      })
    } catch (error) {
      hilog.error(0x0000, 'testTag', 'Start ability error caught %{public}s', JSON.stringify(error));
    }

    let result: insightIntent.ExecuteResult = {
      code: 0,
      result: {
        message: 'Execute insight intent succeed.',
      }
    };
    return result;
  }
}

Button() {
  Text('按钮跳转')
    .fontWeight(FontWeight.Bold)
    .maxLines(1)
    .maxFontSize(15)
    .minFontSize(9)
}.type(ButtonType.Capsule)
.layoutWeight(1)
.height('100%')
.margin({
  left: '4vp',
  right: '4vp'
})
.backgroundColor('#0ddffb')
.onClick(async () => {
  Logger.info(TAG, 'onclick--------');
  let intent: InsightIntentExecutorImpl = new InsightIntentExecutorImpl()
  Logger.info(TAG, 'intent result=',JSON.stringify(intent.onExecuteInUIAbilityForegroundMode('xxx', {}, (getContext() as common.UIAbilityContext).windowStage
  )))
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进