0

我正在使用一个Service并且我动态注册一个接收器,例如

private BroadcastReceiver yourCallReceiver;
 yourCallReceiver = new CallReceiver();

        // Registers the receiver so that your service will listen for broadcasts
        registerReceiver(yourCallReceiver, theCallFilter);

并取消注册

@Override
    public void onDestroy() {
        super.onDestroy();

        // Do not forget to unregister the receiver!!!
        unregisterReceiver(yourCallReceiver);
    }

但是我遇到问题,当我取消注册接收器时我需要关闭一些工作,但接收器没有onDestroy方法。我怎样才能做到这一点?取消注册服务中的接收器时,如何停止某些第三方工作?这甚至可能吗?

4

1 回答 1

2

在您的自定义类中添加一个名为onDestroy(或任何您想调用的方法)的方法CallReceiver,如下所示:

public void onDestroy(){
  //Write your clean-up code here
}

在您的 Service 中onDestroy,调用onDestroy您的接收器对象:

public void onDestroy() {
    super.onDestroy();
    // Do not forget to unregister the receiver!!!
    unregisterReceiver(yourCallReceiver);
    yourCallReceiver.onDestroy();
}
于 2012-10-15T10:50:19.797 回答