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 351. In the **ets** directory of the target module, right-click and choose **New > Directory** to create a directory named **EnterpriseExtAbility**. 362. Right-click the **EnterpriseExtAbility** directory and choose **New > TypeScript File** to create a file named **EnterpriseExtAbility.ts**. 373. 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. 38 39```ts 40import EnterpriseAdminExtensionAbility from '@ohos.enterprise.EnterpriseAdminExtensionAbility'; 41 42export default class EnterpriseAdminAbility extends EnterpriseAdminExtensionAbility { 43 onAdminEnabled() { 44 console.info("onAdminEnabled"); 45 } 46 47 onAdminDisabled() { 48 console.info("onAdminDisabled"); 49 } 50 51 onBundleAdded(bundleName: string) { 52 console.info("EnterpriseAdminAbility onBundleAdded bundleName:" + bundleName); 53 } 54 55 onBundleRemoved(bundleName: string) { 56 console.info("EnterpriseAdminAbility onBundleRemoved bundleName" + bundleName); 57 } 58}; 59``` 60 614. 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. 62 63```json 64"extensionAbilities": [ 65 { 66 "name": "ohos.samples.enterprise_admin_ext_ability", 67 "type": "enterpriseAdmin", 68 "exported": true, 69 "srcEntry": "./ets/enterpriseextability/EnterpriseAdminAbility.ts" 70 } 71 ] 72``` 73 74## Example 75 76Use **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**. 77 78```ts 79import adminManager from '@ohos.enterprise.adminManager'; 80import Want from '@ohos.app.ability.Want'; 81import { BusinessError } from '@ohos.base'; 82 83async function subscribeManagedEventCallback() { 84 let admin: Want = { 85 bundleName: 'com.example.myapplication', 86 abilityName: 'EntryAbility', 87 } 88 adminManager.subscribeManagedEvent(admin, 89 [adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_ADDED, 90 adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_REMOVED], (error) => { 91 if (error) { 92 console.error(`Failed to subscribe managed event. Code: ${error.code}, message: ${error.message}`); 93 } else { 94 console.log('Succeeded in subscribing managed event'); 95 } 96 }) 97} 98 99async function unsubscribeManagedEventPromise() { 100 let admin: Want = { 101 bundleName: 'com.example.myapplication', 102 abilityName: 'EntryAbility', 103 } 104 await adminManager.unsubscribeManagedEvent(admin, 105 [adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_ADDED, 106 adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_REMOVED]).then(() => { 107 console.log('Succeeded in subscribing managed event'); 108 }).catch((error: BusinessError) => { 109 console.error(`Failed to subscribe managed event. Code: ${error.code}, message: ${error.message}`); 110 }) 111} 112``` 113 114