1# @ohos.application.StaticSubscriberExtensionContext (StaticSubscriberExtensionContext) 2<!--Kit: Basic Services Kit--> 3<!--Subsystem: Notification--> 4<!--Owner: @michael_woo888--> 5<!--Designer: @dongqingran; @wulong158--> 6<!--Tester: @wanghong1997--> 7<!--Adviser: @huipeizi--> 8 9The **StaticSubscriberExtensionContext** module, inherited from **ExtensionContext**, provides context for StaticSubscriberExtensionAbilities. 10 11You can use the APIs of this module to start StaticSubscriberExtensionAbilities. 12 13> **NOTE** 14> 15> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 16> 17> The APIs of this module can be used only in the stage model. 18> 19> The APIs provided by this module are system APIs. 20 21## Modules to Import 22 23```ts 24import { StaticSubscriberExtensionContext } from '@kit.BasicServicesKit'; 25``` 26 27## Usage 28 29Before using the **StaticSubscriberExtensionContext** module, you must first obtain a **StaticSubscriberExtensionAbility** instance. 30 31```ts 32import { StaticSubscriberExtensionAbility, StaticSubscriberExtensionContext } from '@kit.BasicServicesKit'; 33``` 34 35## StaticSubscriberExtensionContext.startAbility 36 37startAbility(want: Want, callback: AsyncCallback<void>): void 38 39Starts an ability that belongs to the same application as this StaticSubscriberExtensionAbility. This API uses an asynchronous callback to return the result. 40 41Observe the following when using this API: 42 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 43 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 44 45**Required permissions**: ohos.permission.START_ABILITIES_FROM_BACKGROUND 46 47**System capability**: SystemCapability.Ability.AbilityRuntime.Core 48 49**System API**: This is a system API. 50 51**Parameters** 52 53| Name | Type | Mandatory| Description | 54| -------- | ----------------------------------- | ---- | -------------------------- | 55| want | [Want](../apis-ability-kit/js-apis-wantAgent.md) | Yes | Want information about the target ability. | 56| callback | AsyncCallback<void> | Yes | Callback used to return the result.| 57 58**Error codes** 59 60For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](../apis-ability-kit/errorcode-ability.md). 61 62| ID| Error Message | 63| -------- | ------------------------------------------------------------ | 64| 201 | The application does not have permission to call the interface. | 65| 202 | The application is not system-app, can not use system-api. | 66| 401 | Params error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 67| 16000001 | The specified ability does not exist. | 68| 16000002 | Incorrect ability type. | 69| 16000004 | Cannot start an invisible component. | 70| 16000005 | The specified process does not have the permission. | 71| 16000006 | Cross-user operations are not allowed. | 72| 16000008 | The crowdtesting application expires. | 73| 16000009 | An ability cannot be started or stopped in Wukong mode. | 74| 16000011 | The context does not exist. | 75| 16000050 | Internal error. | 76| 16000053 | The ability is not on the top of the UI. | 77| 16000055 | Installation-free timed out. | 78| 16200001 | The caller has been released. | 79| 16300003 | The target application is not the current application. | 80 81**Example** 82 83 ```ts 84import { commonEventManager, BusinessError } from '@kit.BasicServicesKit'; 85import { Want } from '@kit.AbilityKit'; 86 87let want: Want = { 88 bundleName: "com.example.myapp", 89 abilityName: "MyAbility" 90}; 91 92class MyStaticSubscriberExtensionAbility extends StaticSubscriberExtensionAbility { 93 onReceiveEvent(event: commonEventManager.CommonEventData) { 94 console.info(`onReceiveEvent, event: ${JSON.stringify(event)}`); 95 96 try { 97 this.context.startAbility(want, (error: BusinessError) => { 98 if (error) { 99 // Process service logic errors. 100 console.error(`startAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}.`); 101 return; 102 } 103 // Carry out normal service processing. 104 console.info('startAbility succeed'); 105 }); 106 } catch (paramError) { 107 // Process input parameter errors. 108 let code = (paramError as BusinessError).code; 109 let message = (paramError as BusinessError).message; 110 console.error(`startAbility failed, error.code: ${JSON.stringify(code)}, error.message: ${JSON.stringify(message)}.`); 111 } 112 } 113} 114 ``` 115 116## StaticSubscriberExtensionContext.startAbility 117 118startAbility(want: Want): Promise<void> 119 120Starts an ability that belongs to the same application as this StaticSubscriberExtensionAbility. This API uses a promise to return the result. 121 122Observe the following when using this API: 123 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 124 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 125 126**Required permissions**: ohos.permission.START_ABILITIES_FROM_BACKGROUND 127 128**System capability**: SystemCapability.Ability.AbilityRuntime.Core 129 130**System API**: This is a system API. 131 132**Parameters** 133 134| Name| Type | Mandatory| Description | 135| ------ | ----------------------------------- | ---- | ----------------------- | 136| want | [Want](../apis-ability-kit/js-apis-wantAgent.md) | Yes | Want information about the target ability.| 137 138**Return value** 139 140| Type | Description | 141| ------------------- | ------------------------- | 142| Promise<void> | Promise used to return the result.| 143 144**Error codes** 145 146For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](../apis-ability-kit/errorcode-ability.md). 147 148| ID| Error Message | 149| -------- | ------------------------------------------------------------ | 150| 201 | The application does not have permission to call the interface. | 151| 202 | The application is not system-app, can not use system-api. | 152| 401 | Params error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 153| 16000001 | The specified ability does not exist. | 154| 16000002 | Incorrect ability type. | 155| 16000004 | Cannot start an invisible component. | 156| 16000005 | The specified process does not have the permission. | 157| 16000006 | Cross-user operations are not allowed. | 158| 16000008 | The crowdtesting application expires. | 159| 16000009 | An ability cannot be started or stopped in Wukong mode. | 160| 16000011 | The context does not exist. | 161| 16000050 | Internal error. | 162| 16000053 | The ability is not on the top of the UI. | 163| 16000055 | Installation-free timed out. | 164| 16200001 | The caller has been released. | 165| 16300003 | The target application is not the current application. | 166 167**Example** 168 169 ```ts 170import { commonEventManager, BusinessError } from '@kit.BasicServicesKit'; 171import { Want } from '@kit.AbilityKit'; 172 173let want: Want = { 174 bundleName: "com.example.myapp", 175 abilityName: "MyAbility" 176}; 177 178class MyStaticSubscriberExtensionAbility extends StaticSubscriberExtensionAbility { 179 onReceiveEvent(event: commonEventManager.CommonEventData) { 180 console.info(`onReceiveEvent, event: ${JSON.stringify(event)}`); 181 try { 182 this.context.startAbility(want) 183 .then(() => { 184 // Carry out normal service processing. 185 console.info('startAbility succeed'); 186 }) 187 .catch((error: BusinessError) => { 188 // Process service logic errors. 189 console.error(`startAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}.`); 190 }); 191 } catch (paramError) { 192 // Process input parameter errors. 193 let code = (paramError as BusinessError).code; 194 let message = (paramError as BusinessError).message; 195 console.error(`startAbility failed, error.code: ${JSON.stringify(code)}, error.message: ${JSON.stringify(message)}.`); 196 } 197 } 198} 199 ``` 200