I'm using a foreground Android Service
to request a resource with Retrofit, via REST API, as a timer every 60 seconds. Such service is also running in a different process than the app, this is necessary so that Android doesn't kill it when falls into Doze mode.
public class ResourceService extends Service {
...
private BroadcastReceiver mReceiver;
@Override
public void onCreate() {
super.onCreate();
// create mReceiver (BroadcastReceiver)
// create intentFilter (IntentFilter)
registerReceiver(mReceiver, intentFilter);
}
...
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// main logic
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
}
When I logout the app, onDestroy()
method is called, so unregisterReceiver()
is called too. The app goes to the login activity as expected and the service successfully stops. However, logcat arises the following Exception:
09-18 09:30:06.627 849-849/foo.mycompany.es.myapp:externalProcess E/ActivityThread: Service foo.mycompany.es.myapp.resources.ResourcesService has leaked IntentReceiver foo.mycompany.es.myapp.resources.ResourcesService$2@7c90278 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Service foo.mycompany.es.myapp.resources.ResourcesService has leaked IntentReceiver foo.mycompany.es.myapp.resources.ResourcesService$2@7c90278 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1222)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:993)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1348)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1328)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1322)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:630)
at foo.mycompany.es.myapp.resources.ResourcesService.registrarReceptor(ResourcesService.java:104)
at foo.mycompany.es.myapp.resources.ResourcesService.onCreate(ResourcesService.java:74)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3534)
at android.app.ActivityThread.-wrap6(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1732)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
I have seen a similar answer on the topic but it refers to activities. Such answer states unregisterReceiver()
should be placed in onPause()
method, but there is no such method in Service
superclass which I could override.
Does anybody know what is the cause of the above error?
Thank you in advance.
Call unregisterReceiver(locationReceiver) in onstartcommand
Try unregister receiver before calling super.onDestroy()
@Override
public void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With