1# @ohos.app.ability.FenceExtensionContext (FenceExtensionContext) (System API) 2 3The **FenceExtensionContext** class defines the context for **FenceExtensionAbility** objects. Inherited from [ExtensionContext](../apis-ability-kit/js-apis-inner-application-extensionContext.md), this class provides the configuration information of **FenceExtensionAbility** objects and the API for starting them. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 14. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> The APIs of this module can be used only in the stage model. 9> The APIs provided by this module are system APIs. 10 11## Modules to Import 12 13```ts 14import { FenceExtensionContext } from '@kit.LocationKit'; 15``` 16 17## Usage 18 19Before using **FenceExtensionContext**, you must first obtain a **FenceExtensionAbility** instance. 20 21```ts 22import { FenceExtensionAbility, FenceExtensionContext } from '@kit.LocationKit'; 23 24class MyFenceExtensionAbility extends FenceExtensionAbility { 25 onCreate() { 26 let fenceExtensionContext: FenceExtensionContext = this.context; 27 } 28} 29``` 30 31## FenceExtensionContext.startAbility 32 33startAbility(want: Want): Promise<void> 34 35Starts an ability. This API uses a promise to return the result. It can be called only by the main thread. 36 37**System API**: This is a system API. 38 39**System capability**: SystemCapability.Location.Location.Geofence 40 41**Parameters** 42 43| Name| Type | Mandatory| Description | 44| ------| --------------------------------- | ---- | -------------------------------------- | 45| want| [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | Want information about the target ability.| 46 47**Returns** 48 49| Type | Description | 50| ------------ | ---------------------------------- | 51| Promise<void> | Promise used to return the result.| 52 53**Error codes** 54 55| ID| Error Message | 56| -------- | ------------------------------------------------------------ | 57| 202 | The application is not system-app, can not use system-api. | 58| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 59| 16000001 | The specified ability does not exist. | 60| 16000002 | Incorrect ability type. | 61| 16000004 | Can not start invisible component. | 62| 16000005 | The specified process does not have the permission. | 63| 16000006 | Cross-user operations are not allowed. | 64| 16000008 | The crowdtesting application expires. | 65| 16000009 | An ability cannot be started or stopped in Wukong mode. | 66| 16000011 | The context does not exist. | 67| 16000050 | Internal error. | 68| 16000053 | The ability is not on the top of the UI. | 69| 16000055 | Installation-free timed out. | 70| 16200001 | The caller has been released. | 71| 16300003 | The target application is not self application. | 72 73For details about the error codes, see [Ability Error Codes](../apis-ability-kit/errorcode-ability.md). 74 75**Example** 76 77```ts 78import { FenceExtensionAbility, FenceExtensionContext, geoLocationManager } from '@kit.LocationKit'; 79import { BusinessError } from '@kit.BasicServicesKit'; 80 81class MyFenceExtensionAbility extends FenceExtensionAbility { 82 onFenceStatusChange(transition: geoLocationManager.GeofenceTransition, additions: Record<string, string>): void { 83 // Receive the geofence status change event and process the service logic. 84 hilog.info(0x0000, "TAG", 85 `on geofence transition,id:${transition.geofenceId},event:${transition.transitionEvent},additions:${JSON.stringify(additions)}`); 86 let want: Want = { 87 bundleName: "com.example.myapp", 88 abilityName: "MyServiceExtensionAbility" 89 }; 90 try { 91 this.context.startAbility(want) 92 .then(() => { 93 // Carry out normal service processing. 94 hilog.info(0x0000, "TAG", 'startAbility succeed'); 95 }) 96 .catch((error: BusinessError) => { 97 // Process service logic errors. 98 hilog.info(0x0000, "TAG", 'startAbility failed, error.code: ' + JSON.stringify(error.code) + 99 ' error.message: ' + JSON.stringify(error.message)); 100 }); 101 } catch (paramError) { 102 // Process input parameter errors. 103 let code = (paramError as BusinessError).code; 104 let message = (paramError as BusinessError).message; 105 hilog.info(0x0000, "TAG", 'startAbility failed, error.code: ' + JSON.stringify(code) + 106 ' error.message: ' + JSON.stringify(message)); 107 } 108 } 109} 110``` 111