接收器中不存在onDestroy方法。什么是替代品?

时间:2012-10-15 09:02:13

标签: android

我正在使用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方法。我怎样才能做到这一点?如何取消注册服务器中的接收器,我怎么能停止一些第三方工作?这有可能吗?

1 个答案:

答案 0 :(得分:2)

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

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

在您的服务onDestroy中,拨打接收方对象上的onDestroy

public void onDestroy() {
    super.onDestroy();
    // Do not forget to unregister the receiver!!!
    unregisterReceiver(yourCallReceiver);
    yourCallReceiver.onDestroy();
}