1# EnterpriseAdminExtensionAbility 2 3## Introduction to EnterpriseAdminExtensionAbility 4 5EnterpriseAdminExtensionAbility is a mandatory component for Mobile Device Management (MDM) applications. When developing MDM applications for enterprises, you need to inherit EnterpriseAdminExtensionAbility and implement MDM service logic in the EnterpriseAdminExtensionAbility instance. EnterpriseAdminExtensionAbility implements notifications of system management status changes and defines the callbacks for when a device administrator application is enabled or disabled or an application is installed or uninstalled. 6 7## Constraints 8 9EnterpriseAdminExtensionAbility is applicable only to enterprise administrator applications. 10 11 12## Observing Activation/Deactivation of a Device Administrator Application and Installation/Uninstallation of an Application 13 14### Overview 15 16**onAdminEnabled**: called when an enterprise administrator or employee deploys an MDM application and enables the DeviceAdmin permission for the application. The MDM application can set the initialization policy in the **onAdminEnabled** callback. 17 18**onAdminDisabled**: called when the system or employee disables the DeviceAdmin permission to notify the enterprise administrator that the device is no longer managed. 19 20**onBundleAdded**: The enterprise administrator can subscribe to application installation events. When an application is installed on an enterprise device, the MDM application reports the event in this callback to notify the enterprise administrator. 21 22**onBundleRemoved**: The enterprise administrator can subscribe to application uninstallation events. When an application is uninstalled on an enterprise device, the MDM application reports the event in this callback to notify the enterprise administrator. 23 24### Available APIs 25 26| Class | API | Description | 27| ------------------------------ | ----------------------------------------- | ---------------------------- | 28| EnterpriseAdminExtensionAbility | onAdminEnabled(): void | Called when the device administrator application is disabled. | 29| EnterpriseAdminExtensionAbility | onAdminDisabled(): void | Called when the device administrator application is enabled.| 30| EnterpriseAdminExtensionAbility | onBundleAdded(bundleName: string): void | Called when the MDM application is installed. | 31| EnterpriseAdminExtensionAbility | onBundleRemoved(bundleName: string): void | Called when the MDM application is uninstalled. | 32 33### How to Develop 34 35To implement EnterpriseAdminExtensionAbility, you need to activate the device administrator application and create **ExtensionAbility** in the code directory of the device administrator application. The procedure is as follows: 36 371. In the **ets** directory of the target module, right-click and choose **New > Directory** to create a directory named **EnterpriseExtAbility**. 382. Right-click the **EnterpriseExtAbility** directory and choose **New > TypeScript File** to create a file named **EnterpriseExtAbility.ts**. 393. Open the **EnterpriseExtAbility.ts** file and import the **EnterpriseAdminExtensionAbility** module. Customize a class that inherits from **EnterpriseAdminExtensionAbility** and add the required callbacks, such as **onAdminEnabled()** and **onAdminDisabled()**, When the device administrator application is activated or deactivated, the device administrator can receive notifications. 40 41 ```ts 42 import EnterpriseAdminExtensionAbility from '@ohos.enterprise.EnterpriseAdminExtensionAbility'; 43 44 export default class EnterpriseAdminAbility extends EnterpriseAdminExtensionAbility { 45 46 onAdminEnabled() { 47 console.info("onAdminEnabled"); 48 } 49 50 onAdminDisabled() { 51 console.info("onAdminDisabled"); 52 } 53 54 onBundleAdded(bundleName: string) { 55 console.info("EnterpriseAdminAbility onBundleAdded bundleName:" + bundleName) 56 } 57 58 onBundleRemoved(bundleName: string) { 59 console.info("EnterpriseAdminAbility onBundleRemoved bundleName" + bundleName) 60 } 61 }; 62 ``` 63 644. Register **ServiceExtensionAbility** in the [**module.json5**](../quick-start/module-configuration-file.md) file corresponding to the project module. Set **type** to **enterpriseAdmin** and **srcEntry** to the path of the ExtensionAbility code. 65 66 ```ts 67 "extensionAbilities": [ 68 { 69 "name": "ohos.samples.enterprise_admin_ext_ability", 70 "type": "enterpriseAdmin", 71 "exported": true, 72 "srcEntry": "./ets/enterpriseextability/EnterpriseAdminAbility.ts" 73 } 74 ] 75 ``` 76 77## Example 78 79Use **subscribeManagedEvent** in the **@ohos.enterprise.adminManager** module to subscribe to application installation and removal events. When an application is installed or removed, the MDM application is notified of the event. Then, the MDM application reports the event in the callback to notify the enterprise administrator. To unsubscribe from events, use **unsubscribeManagedEvent**. 80 81```ts 82 @State managedEvents: Array<adminManager.ManagedEvent> = [0,1] 83 @State subscribeManagedEventMsg: string = "" 84 @State unsubscribeManagedEventMsg: string = "" 85 86 async subscribeManagedEventCallback() { 87 await adminManager.subscribeManagedEvent(this.admin, 88 [adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_ADDED, 89 adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_REMOVED], (error) => { 90 if (error) { 91 this.subscribeManagedEventMsg = 'subscribeManagedEvent Callback::errorCode: ' + error.code + ' errorMessage: ' + error.message 92 } else { 93 this.subscribeManagedEventMsg = 'subscribeManagedEvent Callback::success' 94 } 95 }) 96 } 97 98 async unsubscribeManagedEventPromise() { 99 await adminManager.unsubscribeManagedEvent(this.admin, 100 [adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_ADDED, 101 adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_REMOVED]).then(() => { 102 this.unsubscribeManagedEventMsg = 'unsubscribeManagedEvent Promise::success' 103 }).catch((error) => { 104 this.unsubscribeManagedEventMsg = 'unsubscribeManagedEvent Promise::errorCode: ' + error.code + ' errorMessage: ' + error.message 105 }) 106 } 107``` 108 109