• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.selectionInput.SelectionExtensionContext (划词扩展上下文)(系统接口)
2
3SelectionExtensionContext是[SelectionExtensionAbility](./js-apis-selectionInput-selectionExtensionAbility-sys.md)的上下文,继承自[ExtensionContext](../apis-ability-kit/js-apis-inner-application-extensionContext.md)。
4
5每个SelectionExtensionAbility组件实例化时,系统都会自动创建对应的SelectionExtensionContext。开发者可以通过SelectionExtensionContext拉起同应用内其他Ability。
6
7> **说明:**
8>
9> - 本模块首批接口从API version 20开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
10> - 本模块接口为系统接口。
11
12## 导入模块
13
14```ts
15import { SelectionExtensionContext } from '@kit.BasicServicesKit';
16```
17
18## SelectionExtensionContext
19
20### startAbility
21
22startAbility(want: Want): Promise\<void>
23
24拉起目标应用。使用Promise异步回调。
25
26**系统能力:** SystemCapability.SelectionInput.Selection
27
28**参数:**
29
30| 参数名 | 类型                                                    | 必填 | 说明                                                         |
31| ------ | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
32| want   | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 想要被拉起的应用信息,包括Ability名称、Bundle名称等。 |
33
34**返回值:**
35
36| 类型           | 说明                      |
37| -------------- | ------------------------- |
38| Promise\<void> | Promise对象,无返回结果。 |
39
40**错误码:**
41
42以下错误码的详细介绍请参见[元能力错误码](../apis-ability-kit/errorcode-ability.md)。
43
44| 错误码ID | 错误信息                                                |
45| -------- | ------------------------------------------------------- |
46| 16000001 | The specified ability does not exist.                   |
47| 16000002 | Incorrect ability type.                                 |
48| 16000004 | Cannot start an invisible component.                    |
49| 16000005 | The specified process does not have the permission.     |
50| 16000006 | Cross-user operations are not allowed.                  |
51| 16000008 | The crowdtesting application expires.                   |
52| 16000009 | An ability cannot be started or stopped in Wukong mode. |
53| 16000010 | The call with the continuation and prepare continuation flag is forbidden.       |
54| 16000011 | The context does not exist.                             |
55| 16000012 | The application is controlled.                          |
56| 16000013 | The application is controlled by EDM.                   |
57| 16000019 | No matching ability is found.                            |
58| 16000050 | Internal error.                                         |
59| 16000053 | The ability is not on the top of the UI.                |
60| 16000055 | Installation-free timed out.                            |
61| 16000061 | Operation not supported.                                |
62| 16000069 | The extension cannot start the third party application. |
63| 16000070 | The extension cannot start the service.                 |
64| 16000083 | The ExtensionAbility cannot start the ability due to system control.                 |
65| 16200001 | The caller has been released.                           |
66
67**示例:**
68
69```ts
70import { SelectionExtensionAbility, BusinessError } from '@kit.BasicServicesKit';
71import { rpc } from '@kit.IPCKit';
72import { Want } from '@kit.AbilityKit';
73
74class SelectionAbilityStub extends rpc.RemoteObject {
75  constructor(des: string) {
76    super(des);
77  }
78  onRemoteMessageRequest(
79    code: number,
80    data: rpc.MessageSequence,
81    reply: rpc.MessageSequence,
82    options: rpc.MessageOption
83  ): boolean | Promise<boolean> {
84    console.info(`onRemoteMessageRequest code: ${code}`);
85    return true;
86  }
87}
88
89class SelectionExtAbility extends SelectionExtensionAbility {
90  onConnect(want: Want): rpc.RemoteObject {
91    try {
92      let wantAbility: Want = {
93        bundleName: "com.selection.selectionapplication",
94        abilityName: "EntryAbility",
95      };
96      this.context.startAbility(wantAbility).then(() => {
97        console.info(`startAbility success`);
98      }).catch((err: BusinessError) => {
99        let error = err as BusinessError;
100        console.error(`startAbility error: ${error.code} ${error.message}`);
101      })
102    } catch (err) {
103      let error = err as BusinessError;
104      console.error(`startAbility error: ${error.code} ${error.message}`);
105    }
106    return new SelectionAbilityStub('remote');
107  }
108}
109```