```
class DeviceStatusClient final : public DelayedRefSingleton<DeviceStatusClient> {
DECLARE_DELAYED_REF_SINGLETON(DeviceStatusClient)
public:
~DeviceStatusClient();
ErrCode Connect();
if (remoteObject->IsProxyObject()) {
remoteObject->AddDeathRecipient(deathRecipient_);
}
proxy_ = iface_cast<Idevicestatus>(remoteObject);
return RET_OK;
}
```
### 步骤5. 客户端析构时,删除消亡信息接收者
```
DeviceStatusClient::~DeviceStatusClient()
{
if (proxy_ != nullptr) {
auto remoteObject = proxy_->AsObject();
if (remoteObject != nullptr) {
remoteObject->RemoveDeathRecipient(deathRecipient_);
}
}
}
```
## 例3:服务端监听客户端消亡
### 在本端要做的事情
定义接收者对象、添加接收者、删除接收者、处理消亡事件
#### 步骤1. 在服务侧(本端)定义一个消亡信息接收者对象
```
class SensorService : public SystemAbility, public StreamServer, public SensorServiceStub {
DECLARE_SYSTEM_ABILITY(SensorService)
SENSOR_DECLARE_DELAYED_SP_SINGLETON(SensorService);