1e41f4b71Sopenharmony_ci# @ohos.application.StaticSubscriberExtensionContext (StaticSubscriberExtensionContext)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciStaticSubscriberExtensionContext模块是StaticSubscriberExtensionAbility的上下文环境,继承自ExtensionContext。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciStaticSubscriberExtensionContext模块提供StaticSubscriberExtensionAbility具有的接口和能力。
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci> **说明:**
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
10e41f4b71Sopenharmony_ci>
11e41f4b71Sopenharmony_ci> 本模块接口仅可在Stage模型下使用。
12e41f4b71Sopenharmony_ci>
13e41f4b71Sopenharmony_ci> 本模块接口均为系统接口。
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## 导入模块
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci```ts
18e41f4b71Sopenharmony_ciimport { StaticSubscriberExtensionContext } from '@kit.BasicServicesKit';
19e41f4b71Sopenharmony_ci```
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci## 使用说明
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci在使用StaticSubscriberExtensionContext的功能前,需要通过StaticSubscriberExtensionAbility获取。
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci```ts
26e41f4b71Sopenharmony_ciimport { StaticSubscriberExtensionAbility, StaticSubscriberExtensionContext } from '@kit.BasicServicesKit';
27e41f4b71Sopenharmony_ci```
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci## StaticSubscriberExtensionContext.startAbility
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_cistartAbility(want: Want, callback: AsyncCallback<void>): void;
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci拉起一个静态订阅所属的同应用的Ability。使用callback异步回调。
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci使用规则:
36e41f4b71Sopenharmony_ci - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
37e41f4b71Sopenharmony_ci - 跨应用场景下,目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.START_ABILITIES_FROM_BACKGROUND
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci**系统应用**:该接口为系统接口。
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci**参数:**
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci| 参数名   | 类型                                | 必填 | 说明                       |
48e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---- | -------------------------- |
49e41f4b71Sopenharmony_ci| want     | [Want](../apis-ability-kit/js-apis-wantAgent.md) | 是   | 启动Ability的want信息。    |
50e41f4b71Sopenharmony_ci| callback | AsyncCallback<void>           | 是   | callback形式返回启动结果。 |
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci**错误码:**
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                                     |
55e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
56e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
57e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist.                        |
58e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type.                                      |
59e41f4b71Sopenharmony_ci| 16000004 | Can not start invisible component.                           |
60e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission.          |
61e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed.                       |
62e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires.                        |
63e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode.      |
64e41f4b71Sopenharmony_ci| 16000011 | The context does not exist.                                  |
65e41f4b71Sopenharmony_ci| 16000050 | Internal error.                                              |
66e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI.                     |
67e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out.                                 |
68e41f4b71Sopenharmony_ci| 16200001 | The caller has been released.                                |
69e41f4b71Sopenharmony_ci| 16300003 | The target application is not self application.              |
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci以上错误码详细介绍请参考[元能力子系统错误码](../apis-ability-kit/errorcode-ability.md)。
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci**示例:**
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci  ```ts
76e41f4b71Sopenharmony_ciimport { commonEventManager, BusinessError } from '@kit.BasicServicesKit';
77e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit';
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_cilet want: Want = {
80e41f4b71Sopenharmony_ci  bundleName: "com.example.myapp",
81e41f4b71Sopenharmony_ci  abilityName: "MyAbility"
82e41f4b71Sopenharmony_ci};
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ciclass MyStaticSubscriberExtensionAbility extends StaticSubscriberExtensionAbility {
85e41f4b71Sopenharmony_ci  onReceiveEvent(event: commonEventManager.CommonEventData) {
86e41f4b71Sopenharmony_ci    console.log(`onReceiveEvent, event: ${JSON.stringify(event)}`);
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci    try {
89e41f4b71Sopenharmony_ci      this.context.startAbility(want, (error: BusinessError) => {
90e41f4b71Sopenharmony_ci        if (error) {
91e41f4b71Sopenharmony_ci          // 处理业务逻辑错误
92e41f4b71Sopenharmony_ci          console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
93e41f4b71Sopenharmony_ci            ' error.message: ' + JSON.stringify(error.message));
94e41f4b71Sopenharmony_ci          return;
95e41f4b71Sopenharmony_ci        }
96e41f4b71Sopenharmony_ci        // 执行正常业务
97e41f4b71Sopenharmony_ci        console.log('startAbility succeed');
98e41f4b71Sopenharmony_ci      });
99e41f4b71Sopenharmony_ci    } catch (paramError) {
100e41f4b71Sopenharmony_ci      // 处理入参错误异常
101e41f4b71Sopenharmony_ci      let code = (paramError as BusinessError).code;
102e41f4b71Sopenharmony_ci      let message = (paramError as BusinessError).message;
103e41f4b71Sopenharmony_ci      console.log('startAbility failed, error.code: ' + JSON.stringify(code) +
104e41f4b71Sopenharmony_ci        ' error.message: ' + JSON.stringify(message));
105e41f4b71Sopenharmony_ci    }
106e41f4b71Sopenharmony_ci  }
107e41f4b71Sopenharmony_ci}
108e41f4b71Sopenharmony_ci  ```
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci## StaticSubscriberExtensionContext.startAbility
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_cistartAbility(want: Want): Promise<void>;
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci拉起一个静态订阅所属的同应用的Ability。使用Promise异步回调。
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci使用规则:
117e41f4b71Sopenharmony_ci - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
118e41f4b71Sopenharmony_ci - 跨应用场景下,目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.START_ABILITIES_FROM_BACKGROUND
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci**系统应用**:该接口为系统接口。
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci**参数:**
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci| 参数名 | 类型                                | 必填 | 说明                    |
129e41f4b71Sopenharmony_ci| ------ | ----------------------------------- | ---- | ----------------------- |
130e41f4b71Sopenharmony_ci| want   | [Want](../apis-ability-kit/js-apis-wantAgent.md) | 是   | 启动Ability的want信息。 |
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci**返回值:**
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci| 类型                | 说明                      |
135e41f4b71Sopenharmony_ci| ------------------- | ------------------------- |
136e41f4b71Sopenharmony_ci| Promise<void> | Promise形式返回启动结果。 |
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci**错误码:**
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                                     |
141e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
142e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
143e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist.                        |
144e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type.                                      |
145e41f4b71Sopenharmony_ci| 16000004 | Can not start invisible component.                           |
146e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission.          |
147e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed.                       |
148e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires.                        |
149e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode.      |
150e41f4b71Sopenharmony_ci| 16000011 | The context does not exist.                                  |
151e41f4b71Sopenharmony_ci| 16000050 | Internal error.                                              |
152e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI.                     |
153e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out.                                 |
154e41f4b71Sopenharmony_ci| 16200001 | The caller has been released.                                |
155e41f4b71Sopenharmony_ci| 16300003 | The target application is not self application.              |
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ci以上错误码详细介绍请参考[元能力子系统错误码](../apis-ability-kit/errorcode-ability.md)。
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci**示例:**
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci  ```ts
162e41f4b71Sopenharmony_ciimport { commonEventManager, BusinessError } from '@kit.BasicServicesKit';
163e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit';
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_cilet want: Want = {
166e41f4b71Sopenharmony_ci  bundleName: "com.example.myapp",
167e41f4b71Sopenharmony_ci  abilityName: "MyAbility"
168e41f4b71Sopenharmony_ci};
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ciclass MyStaticSubscriberExtensionAbility extends StaticSubscriberExtensionAbility {
171e41f4b71Sopenharmony_ci  onReceiveEvent(event: commonEventManager.CommonEventData) {
172e41f4b71Sopenharmony_ci    console.log(`onReceiveEvent, event: ${JSON.stringify(event)}`);
173e41f4b71Sopenharmony_ci    try {
174e41f4b71Sopenharmony_ci      this.context.startAbility(want)
175e41f4b71Sopenharmony_ci        .then(() => {
176e41f4b71Sopenharmony_ci          // 执行正常业务
177e41f4b71Sopenharmony_ci          console.log('startAbility succeed');
178e41f4b71Sopenharmony_ci        })
179e41f4b71Sopenharmony_ci        .catch((error: BusinessError) => {
180e41f4b71Sopenharmony_ci          // 处理业务逻辑错误
181e41f4b71Sopenharmony_ci          console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
182e41f4b71Sopenharmony_ci            ' error.message: ' + JSON.stringify(error.message));
183e41f4b71Sopenharmony_ci        });
184e41f4b71Sopenharmony_ci    } catch (paramError) {
185e41f4b71Sopenharmony_ci      // 处理入参错误异常
186e41f4b71Sopenharmony_ci      let code = (paramError as BusinessError).code;
187e41f4b71Sopenharmony_ci      let message = (paramError as BusinessError).message;
188e41f4b71Sopenharmony_ci      console.log('startAbility failed, error.code: ' + JSON.stringify(code) +
189e41f4b71Sopenharmony_ci        ' error.message: ' + JSON.stringify(message));
190e41f4b71Sopenharmony_ci    }
191e41f4b71Sopenharmony_ci  }
192e41f4b71Sopenharmony_ci}
193e41f4b71Sopenharmony_ci  ```