harmony 鸿蒙FenceExtensionAbility
FenceExtensionAbility
Overview
FenceExtensionAbility is an ExtensionAbility of the geofence type. It enables you to efficiently implement geofencing functionalities.
When to Use
- Subscribe to geofence status change events through the geoLocationManager.on(‘gnssFenceStatusChange’) API of the location service, and pass the FenceExtensionAbility parameters in the want parameter.
- After the event is triggered, the system reports the geofence event and data through the onFenceStatusChange API. Based on the event received, the application can perform corresponding service processing, for example, sending notifications.
Available APIs
For details about the APIs, see FenceExtensionAbility. |Available APIs|Description| |—-|—-| |onFenceStatusChange(transition: geoLocationManager.GeofenceTransition, additions: Record<string, string>): void|Callback invoked when a geofence status change event is received. Service processing is then performed based on the event type and data.| |onDestroy(): void|Callback invoked when a FenceExtensionAbility destruction event is received.|
How to Develop
Implementing a FenceExtensionAbility
Implement the capability of the FenceExtensionAbility provider.
To manually create a FenceExtensionAbility in a project in DevEco Studio, perform the following steps:
- In the ets directory of a module in the project, right-click and choose New > Directory to create a directory named fenceextensionability.
- Right-click the fenceextensionability directory, and choose New > File to create a file named MyFenceExtensionAbility.ets.
- Open the MyFenceExtensionAbility.ets file and import its dependencies. Customize a class that inherits from FenceExtensionAbility and implement the onFenceStatusChange and onDestroy APIs.
The sample code is as follows:
import { FenceExtensionAbility, geoLocationManager } from '@kit.LocationKit';
import { notificationManager } from '@kit.NotificationKit';
import { Want, wantAgent } from '@kit.AbilityKit';
export class MyFenceExtensionAbility extends FenceExtensionAbility {
onFenceStatusChange(transition: geoLocationManager.GeofenceTransition, additions: Record<string, string>): void {
// Receive the geofence status change event and process the service logic.
console.info(`on geofence transition,id:${transition.geofenceId},event:${transition.transitionEvent},additions:${JSON.stringify(additions)}`);
// Send a geofence notification.
let wantAgentInfo: wantAgent.WantAgentInfo = {
wants: [
{
bundleName: 'com.example.myapplication',
abilityName: 'EntryAbility',
parameters:
{
"geofenceId": transition?.geofenceId,
"transitionEvent": transition?.transitionEvent,
}
} as Want
],
actionType: wantAgent.OperationType.START_ABILITY,
requestCode: 100
};
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentMy) => {
let notificationRequest: notificationManager.NotificationRequest = {
id: 1,
content: {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
title: `Geofence Notification`,
text: `on geofence transition,id:${transition.geofenceId},event:${transition.transitionEvent},additions:${JSON.stringify(additions)}`,
}
},
notificationSlotType: notificationManager.SlotType.SOCIAL_COMMUNICATION,
wantAgent: wantAgentMy
};
notificationManager.publish(notificationRequest);
});
}
}
- Register the FenceExtensionAbility in the module.json5 file of the module in the project. Set type to “fence” and srcEntry to the code path of the FenceExtensionAbility component.
{
"module": {
"extensionAbilities": [
{
"name": "MyFenceExtensionAbility",
"srcEntry": "./ets/fenceExtensionability/MyFenceExtensionAbility.ets",
"description": "MyFenceExtensionAbility",
"type": "fence",
"exported": true
},
]
}
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Geocoding and Reverse Geocoding (ArkTS)
harmony 鸿蒙Obtaining Device Location Information (C/C++)
harmony 鸿蒙Obtaining Device Location Information (ArkTS)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦