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