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