1e41f4b71Sopenharmony_ci# Creating a ServiceAbility 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci1. Create a ServiceAbility. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci When you create a ServiceAbility in DevEco Studio, DevEco Studio automatically generates the **onStart()**, **onStop()**, and **onCommand()** callbacks. You need to implement the other lifecycle callbacks. You can also implement your own logic to be processed when another ability requests to interact with your ServiceAbility. The sample code is as follows: 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci ```ts 9e41f4b71Sopenharmony_ci import { Want } from '@kit.AbilityKit'; 10e41f4b71Sopenharmony_ci import { rpc } from '@kit.IPCKit'; 11e41f4b71Sopenharmony_ci import { hilog } from '@kit.PerformanceAnalysisKit'; 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci const TAG: string = '[Sample_FAModelAbilityDevelop]'; 14e41f4b71Sopenharmony_ci const domain: number = 0xFF00; 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci class FirstServiceAbilityStub extends rpc.RemoteObject { 17e41f4b71Sopenharmony_ci constructor(des: Object) { 18e41f4b71Sopenharmony_ci if (typeof des === 'string') { 19e41f4b71Sopenharmony_ci super(des); 20e41f4b71Sopenharmony_ci } else { 21e41f4b71Sopenharmony_ci return; 22e41f4b71Sopenharmony_ci } 23e41f4b71Sopenharmony_ci } 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption): boolean { 26e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'ServiceAbility onRemoteRequest called'); 27e41f4b71Sopenharmony_ci if (code === 1) { 28e41f4b71Sopenharmony_ci let string = data.readString(); 29e41f4b71Sopenharmony_ci hilog.info(domain, TAG, `ServiceAbility string=${string}`); 30e41f4b71Sopenharmony_ci let result = Array.from(string).sort().join(''); 31e41f4b71Sopenharmony_ci hilog.info(domain, TAG, `ServiceAbility result=${result}`); 32e41f4b71Sopenharmony_ci reply.writeString(result); 33e41f4b71Sopenharmony_ci } else { 34e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'ServiceAbility unknown request code'); 35e41f4b71Sopenharmony_ci } 36e41f4b71Sopenharmony_ci return true; 37e41f4b71Sopenharmony_ci } 38e41f4b71Sopenharmony_ci } 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci class ServiceAbility { 41e41f4b71Sopenharmony_ci onStart(): void { 42e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'ServiceAbility onStart'); 43e41f4b71Sopenharmony_ci } 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci onStop(): void { 46e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'ServiceAbility onStop'); 47e41f4b71Sopenharmony_ci } 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci onCommand(want: Want, startId: number): void { 50e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'ServiceAbility onCommand'); 51e41f4b71Sopenharmony_ci } 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci onConnect(want: Want): rpc.RemoteObject { 54e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'ServiceAbility onDisconnect' + want); 55e41f4b71Sopenharmony_ci return new FirstServiceAbilityStub('test'); 56e41f4b71Sopenharmony_ci } 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci onDisconnect(want: Want): void { 59e41f4b71Sopenharmony_ci hilog.info(domain, TAG, 'ServiceAbility onDisconnect' + want); 60e41f4b71Sopenharmony_ci } 61e41f4b71Sopenharmony_ci } 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci export default new ServiceAbility(); 64e41f4b71Sopenharmony_ci ``` 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci2. Register the ServiceAbility. 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci Declare the ServiceAbility in the **config.json** file by setting its **type** attribute to **service**. The **visible** attribute specifies whether the ServiceAbility can be called by other applications. The value **true** means that the ServiceAbility can be called by other applications, and **false** means that the ServiceAbility can be called only within the application. To enable the ServiceAbility to be called by other applications, set **visible** to **true** when registering the ServiceAbility and enable associated startup. For details about the startup rules, see [Component Startup Rules](component-startup-rules.md). 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci ```json 71e41f4b71Sopenharmony_ci { 72e41f4b71Sopenharmony_ci ... 73e41f4b71Sopenharmony_ci "module": { 74e41f4b71Sopenharmony_ci ... 75e41f4b71Sopenharmony_ci "abilities": [ 76e41f4b71Sopenharmony_ci ... 77e41f4b71Sopenharmony_ci { 78e41f4b71Sopenharmony_ci "name": ".ServiceAbility", 79e41f4b71Sopenharmony_ci "srcLanguage": "ets", 80e41f4b71Sopenharmony_ci "srcPath": "ServiceAbility", 81e41f4b71Sopenharmony_ci "icon": "$media:icon", 82e41f4b71Sopenharmony_ci "description": "$string:ServiceAbility_desc", 83e41f4b71Sopenharmony_ci "type": "service", 84e41f4b71Sopenharmony_ci "visible": true 85e41f4b71Sopenharmony_ci }, 86e41f4b71Sopenharmony_ci ... 87e41f4b71Sopenharmony_ci ] 88e41f4b71Sopenharmony_ci ... 89e41f4b71Sopenharmony_ci } 90e41f4b71Sopenharmony_ci } 91e41f4b71Sopenharmony_ci ``` 92