1e41f4b71Sopenharmony_ci# EnterpriseAdminExtensionAbility 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Introduction to EnterpriseAdminExtensionAbility 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci**EnterpriseAdminExtensionAbility** is a mandatory component for Mobile Device Management (MDM) applications. When developing MDM applications for enterprises, you need to inherit from **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. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Constraints 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci The APIs provided can be used only by device administrator applications. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## Observing Activation/Deactivation of a Device Administrator Application and Installation/Uninstallation of an Application 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci### Introduction 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci- **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. 16e41f4b71Sopenharmony_ci- **onAdminDisabled**: This callback is invoked when the system or an employee deactivates the device administrator. It notifies the application that the DeviceAdmin permission is revoked. The application can inform the enterprise administrator that the device is not under management. 17e41f4b71Sopenharmony_ci- **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. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci- **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. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci### Available APIs 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci| Class | API | Description | 24e41f4b71Sopenharmony_ci| :------------------------------ | ----------------------------------------- | ---------------------------- | 25e41f4b71Sopenharmony_ci| EnterpriseAdminExtensionAbility | onAdminEnabled(): void | Called when the device administrator application is enabled. | 26e41f4b71Sopenharmony_ci| EnterpriseAdminExtensionAbility | onAdminDisabled(): void | Called when the device administrator application is disabled.| 27e41f4b71Sopenharmony_ci| EnterpriseAdminExtensionAbility | onBundleAdded(bundleName: string): void | Called when an MDM application is installed. | 28e41f4b71Sopenharmony_ci| EnterpriseAdminExtensionAbility | onBundleRemoved(bundleName: string): void | Called when an MDM application is uninstalled. | 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci### How to Develop 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci1. In the **ets** directory of the target module, right-click and choose **New > Directory** to create a directory named **EnterpriseExtAbility**. 33e41f4b71Sopenharmony_ci2. Right-click the **EnterpriseExtAbility** directory and choose **New** > **ArkTS File** to create a file named **EnterpriseExtAbility.ets**. 34e41f4b71Sopenharmony_ci3. Open the **EnterpriseExtAbility.ets** 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 enabled or disabled, the callback will be invoked to receive notifications. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci```ts 37e41f4b71Sopenharmony_ciimport EnterpriseAdminExtensionAbility from '@ohos.enterprise.EnterpriseAdminExtensionAbility'; 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ciexport default class EnterpriseAdminAbility extends EnterpriseAdminExtensionAbility { 40e41f4b71Sopenharmony_ci onAdminEnabled() { 41e41f4b71Sopenharmony_ci console.info("onAdminEnabled"); 42e41f4b71Sopenharmony_ci } 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci onAdminDisabled() { 45e41f4b71Sopenharmony_ci console.info("onAdminDisabled"); 46e41f4b71Sopenharmony_ci } 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci onBundleAdded(bundleName: string) { 49e41f4b71Sopenharmony_ci console.info("EnterpriseAdminAbility onBundleAdded bundleName:" + bundleName); 50e41f4b71Sopenharmony_ci } 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci onBundleRemoved(bundleName: string) { 53e41f4b71Sopenharmony_ci console.info("EnterpriseAdminAbility onBundleRemoved bundleName" + bundleName); 54e41f4b71Sopenharmony_ci } 55e41f4b71Sopenharmony_ci}; 56e41f4b71Sopenharmony_ci``` 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci4. Register **ServiceExtensionAbility** in the [module.json5](../quick-start/module-configuration-file.md) file corresponding to the project module, and set **type** to **enterpriseAdmin** and **srcEntry** to the path of the ExtensionAbility code. 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci```json 61e41f4b71Sopenharmony_ci"extensionAbilities": [ 62e41f4b71Sopenharmony_ci { 63e41f4b71Sopenharmony_ci "name": "ohos.samples.enterprise_admin_ext_ability", 64e41f4b71Sopenharmony_ci "type": "enterpriseAdmin", 65e41f4b71Sopenharmony_ci "exported": true, 66e41f4b71Sopenharmony_ci "srcEntry": "./ets/enterpriseextability/EnterpriseAdminAbility.ets" 67e41f4b71Sopenharmony_ci } 68e41f4b71Sopenharmony_ci ] 69e41f4b71Sopenharmony_ci``` 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci## Example 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ciUse **subscribeManagedEvent** in the **@ohos.enterprise.adminManager** module to subscribe to application installation and uninstallation events. After the subscription is successful, the MDM application notifies the enterprise administrator when it is installed or uninstalled on the device. To unsubscribe from events, use **unsubscribeManagedEvent**. 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci```ts 76e41f4b71Sopenharmony_ciimport adminManager from '@ohos.enterprise.adminManager'; 77e41f4b71Sopenharmony_ciimport Want from '@ohos.app.ability.Want'; 78e41f4b71Sopenharmony_ciimport { BusinessError } from '@ohos.base'; 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ciasync function subscribeManagedEventCallback() { 81e41f4b71Sopenharmony_ci let admin: Want = { 82e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 83e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 84e41f4b71Sopenharmony_ci } 85e41f4b71Sopenharmony_ci adminManager.subscribeManagedEvent(admin, 86e41f4b71Sopenharmony_ci [adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_ADDED, 87e41f4b71Sopenharmony_ci adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_REMOVED], (error) => { 88e41f4b71Sopenharmony_ci if (error) { 89e41f4b71Sopenharmony_ci console.error(`Failed to subscribe managed event. Code: ${error.code}, message: ${error.message}`); 90e41f4b71Sopenharmony_ci } else { 91e41f4b71Sopenharmony_ci console.log('Succeeded in subscribing managed event'); 92e41f4b71Sopenharmony_ci } 93e41f4b71Sopenharmony_ci }) 94e41f4b71Sopenharmony_ci} 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ciasync function unsubscribeManagedEventPromise() { 97e41f4b71Sopenharmony_ci let admin: Want = { 98e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 99e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 100e41f4b71Sopenharmony_ci } 101e41f4b71Sopenharmony_ci await adminManager.unsubscribeManagedEvent(admin, 102e41f4b71Sopenharmony_ci [adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_ADDED, 103e41f4b71Sopenharmony_ci adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_REMOVED]).then(() => { 104e41f4b71Sopenharmony_ci console.log('Succeeded in subscribing managed event'); 105e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 106e41f4b71Sopenharmony_ci console.error(`Failed to subscribe managed event. Code: ${error.code}, message: ${error.message}`); 107e41f4b71Sopenharmony_ci }) 108e41f4b71Sopenharmony_ci} 109e41f4b71Sopenharmony_ci``` 110e41f4b71Sopenharmony_ci 111