• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.InsightIntentContext (InsightIntent Call Execution Context)
2
3The **InsightIntentContext** module provides the InsightIntent call execution context, which is an attribute of the base class for InsightIntent call execution and provides basic capabilities for the base class.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```ts
12import InsightIntentContext from '@ohos.app.ability.InsightIntentContext';
13```
14
15## InsightIntentContext.startAbility
16
17startAbility(want: Want, callback: AsyncCallback\<void\>): void
18
19Starts an ability. The ability can be started only when it has the same bundle name as the base class for InsightIntent call execution. This API uses an asynchronous callback to return the result.
20
21**Model restriction**: This API can be used only in the stage model.
22
23**System capability**: SystemCapability.Ability.AbilityRuntime.Core
24
25**Parameters**
26
27| Name| Type| Mandatory| Description|
28| -------- | -------- | -------- | -------- |
29| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
30| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**. Otherwise, **err** is an error object.|
31
32**Error codes**
33
34| ID| Error Message|
35| -------- | -------- |
36| 16000001 | The specified ability does not exist. |
37| 16000004 | Can not start invisible component. |
38| 16000005 | The specified process does not have the permission. |
39| 16000006 | Cross-user operations are not allowed. |
40| 16000008 | The crowdtesting application expires. |
41| 16000009 | An ability cannot be started or stopped in Wukong mode. |
42| 16000011 | The context does not exist.        |
43| 16000012 | The application is controlled.        |
44| 16000013 | The application is controlled by EDM.       |
45| 16000050 | Internal error. |
46| 16000053 | The ability is not on the top of the UI. |
47| 16000055 | Installation-free timed out. |
48| 16000061 | Can not start component belongs to other bundle. |
49| 16200001 | The caller has been released. |
50
51For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
52
53**Example**
54
55  ```ts
56  import IntentExecutor from '@ohos.app.ability.InsightIntentExecutor';
57  import IntentContext from '@ohos.app.ability.InsightIntentContext';
58  import insightIntent from '@ohos.app.ability.insightIntent';
59  import window from '@ohos.window';
60  import Want from '@ohos.app.ability.Want';
61  import hilog from '@ohos.hilog';
62
63  export default class IntentExecutorImpl extends IntentExecutor {
64    onExecuteInUIAbilityForegroundMode(name: string, param: Record<string, Object>, pageLoader: window.WindowStage): insightIntent.ExecuteResult {
65      let want: Want = {
66        bundleName: 'com.ohos.intentexecutedemo',
67        moduleName: 'entry',
68        abilityName: 'AnotherAbility',
69      };
70
71      try {
72        this.context.startAbility(want, (error) => {
73          if (error) {
74            hilog.error(0x0000, 'testTag', 'Start ability failed with %{public}s', JSON.stringify(error));
75          } else {
76            hilog.info(0x0000, 'testTag', '%{public}s', 'Start ability succeed');
77          }
78        })
79      } catch (error) {
80        hilog.error(0x0000, 'testTag', 'Start ability error caught %{public}s', JSON.stringify(error));
81      }
82
83      let result: insightIntent.ExecuteResult = {
84        code: 0,
85        result: {
86          message: 'Execute insight intent succeed.',
87        }
88      };
89      return result;
90    }
91  }
92  ```
93
94## InsightIntentContext.startAbility
95
96startAbility(want: Want): Promise\<void\>
97
98Starts an ability. The ability can be started only when it has the same bundle name as the base class for InsightIntent call execution. This API uses a promise to return the result.
99
100**Model restriction**: This API can be used only in the stage model.
101
102**System capability**: SystemCapability.Ability.AbilityRuntime.Core
103
104**Parameters**
105
106| Name| Type| Mandatory| Description|
107| -------- | -------- | -------- | -------- |
108| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.|
109
110**Return value**
111
112| Type| Description|
113| -------- | -------- |
114| Promise&lt;void&gt; | Promise that returns no value.|
115
116**Error codes**
117
118| ID| Error Message|
119| -------- | -------- |
120| 16000001 | The specified ability does not exist. |
121| 16000004 | Can not start invisible component. |
122| 16000005 | The specified process does not have the permission. |
123| 16000006 | Cross-user operations are not allowed. |
124| 16000008 | The crowdtesting application expires. |
125| 16000009 | An ability cannot be started or stopped in Wukong mode. |
126| 16000011 | The context does not exist.        |
127| 16000012 | The application is controlled.        |
128| 16000013 | The application is controlled by EDM.       |
129| 16000050 | Internal error. |
130| 16000053 | The ability is not on the top of the UI. |
131| 16000055 | Installation-free timed out. |
132| 16000061 | Can not start component belongs to other bundle. |
133| 16200001 | The caller has been released. |
134
135For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
136
137**Example**
138
139  ```ts
140  import IntentExecutor from '@ohos.app.ability.InsightIntentExecutor';
141  import IntentContext from '@ohos.app.ability.InsightIntentContext';
142  import insightIntent from '@ohos.app.ability.insightIntent';
143  import window from '@ohos.window';
144  import Want from '@ohos.app.ability.Want';
145  import hilog from '@ohos.hilog';
146
147  export default class IntentExecutorImpl extends IntentExecutor {
148    async onExecuteInUIAbilityForegroundMode(name: string, param: Record<string, Object>, pageLoader: window.WindowStage): Promise<insightIntent.ExecuteResult> {
149      let want: Want = {
150        bundleName: 'com.ohos.intentexecutedemo',
151        moduleName: 'entry',
152        abilityName: 'AnotherAbility',
153      };
154
155      try {
156        await this.context.startAbility(want);
157          hilog.info(0x0000, 'testTag', '%{public}s', 'Start ability finished');
158        } catch (error) {
159          hilog.error(0x0000, 'testTag', 'Start ability error caught %{public}s', JSON.stringify(error));
160      }
161
162      let result: insightIntent.ExecuteResult = {
163        code: 0,
164        result: {
165          message: 'Execute insight intent succeed.',
166        }
167      };
168      return result;
169    }
170  }
171  ```
172