1# EnterpriseAdminExtensionAbility 2 3## Introduction to EnterpriseAdminExtensionAbility 4 5**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. 6 7## Constraints 8 9 The APIs provided can be used only by device administrator applications. 10 11## Observing Activation/Deactivation of a Device Administrator Application and Installation/Uninstallation of an Application 12 13### Introduction 14 15- **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. 16- **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. 17- **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. 18 19- **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. 20 21### Available APIs 22 23| Class | API | Description | 24| :------------------------------ | ----------------------------------------- | ---------------------------- | 25| EnterpriseAdminExtensionAbility | onAdminEnabled(): void | Called when the device administrator application is enabled. | 26| EnterpriseAdminExtensionAbility | onAdminDisabled(): void | Called when the device administrator application is disabled.| 27| EnterpriseAdminExtensionAbility | onBundleAdded(bundleName: string): void | Called when an MDM application is installed. | 28| EnterpriseAdminExtensionAbility | onBundleRemoved(bundleName: string): void | Called when an MDM application is uninstalled. | 29 30### How to Develop 31 321. In the **ets** directory of the target module, right-click and choose **New > Directory** to create a directory named **EnterpriseExtAbility**. 332. Right-click the **EnterpriseExtAbility** directory and choose **New** > **ArkTS File** to create a file named **EnterpriseExtAbility.ets**. 343. 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. 35 36```ts 37import EnterpriseAdminExtensionAbility from '@ohos.enterprise.EnterpriseAdminExtensionAbility'; 38 39export default class EnterpriseAdminAbility extends EnterpriseAdminExtensionAbility { 40 onAdminEnabled() { 41 console.info("onAdminEnabled"); 42 } 43 44 onAdminDisabled() { 45 console.info("onAdminDisabled"); 46 } 47 48 onBundleAdded(bundleName: string) { 49 console.info("EnterpriseAdminAbility onBundleAdded bundleName:" + bundleName); 50 } 51 52 onBundleRemoved(bundleName: string) { 53 console.info("EnterpriseAdminAbility onBundleRemoved bundleName" + bundleName); 54 } 55}; 56``` 57 584. 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. 59 60```json 61"extensionAbilities": [ 62 { 63 "name": "ohos.samples.enterprise_admin_ext_ability", 64 "type": "enterpriseAdmin", 65 "exported": true, 66 "srcEntry": "./ets/enterpriseextability/EnterpriseAdminAbility.ets" 67 } 68 ] 69``` 70 71## Example 72 73Use **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**. 74 75```ts 76import adminManager from '@ohos.enterprise.adminManager'; 77import Want from '@ohos.app.ability.Want'; 78import { BusinessError } from '@ohos.base'; 79 80async function subscribeManagedEventCallback() { 81 let admin: Want = { 82 bundleName: 'com.example.myapplication', 83 abilityName: 'EntryAbility', 84 } 85 adminManager.subscribeManagedEvent(admin, 86 [adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_ADDED, 87 adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_REMOVED], (error) => { 88 if (error) { 89 console.error(`Failed to subscribe managed event. Code: ${error.code}, message: ${error.message}`); 90 } else { 91 console.log('Succeeded in subscribing managed event'); 92 } 93 }) 94} 95 96async function unsubscribeManagedEventPromise() { 97 let admin: Want = { 98 bundleName: 'com.example.myapplication', 99 abilityName: 'EntryAbility', 100 } 101 await adminManager.unsubscribeManagedEvent(admin, 102 [adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_ADDED, 103 adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_REMOVED]).then(() => { 104 console.log('Succeeded in subscribing managed event'); 105 }).catch((error: BusinessError) => { 106 console.error(`Failed to subscribe managed event. Code: ${error.code}, message: ${error.message}`); 107 }) 108} 109``` 110 111