• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.selectionInput.SelectionExtensionContext (SelectionExtensionContext) (System API)
2
3**SelectionExtensionContext** is the context of [SelectionExtensionAbility](./js-apis-selectionInput-selectionExtensionAbility-sys.md), which is inherited from [ExtensionContext](../apis-ability-kit/js-apis-inner-application-extensionContext.md).
4
5When a SelectionExtensionAbility component is instantiated, the system automatically creates the corresponding **SelectionExtensionContext**. You can use **SelectionExtensionContext** to start other abilities in the same application.
6
7> **NOTE**
8>
9> - The initial APIs of this module are supported since API version 20. Newly added APIs will be marked with a superscript to indicate their earliest API version.
10> - The APIs provided by this module are system APIs.
11
12## Modules to Import
13
14```ts
15import { SelectionExtensionContext } from '@kit.BasicServicesKit';
16```
17
18## SelectionExtensionContext
19
20### startAbility
21
22startAbility(want: Want): Promise\<void>
23
24Starts an ability. This API uses a promise to return the result.
25
26**System capability**: SystemCapability.SelectionInput.Selection
27
28**Parameters**
29
30| Name| Type                                                   | Mandatory| Description                                                        |
31| ------ | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
32| want   | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes  | Want information of the ability to start, including the ability name and bundle name.|
33
34**Return value**
35
36| Type          | Description                     |
37| -------------- | ------------------------- |
38| Promise\<void> | Promise that returns no value.|
39
40**Error codes**
41
42For details about the error codes, see [Ability Error Codes](../apis-ability-kit/errorcode-ability.md).
43
44| ID| Error Message                                               |
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**Example**
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```
110