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