1

Here's the scenario, I have a LoginActivity that makes use of the WifiManager to obtain the IP address like this:

WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
String ipAddress = wifiManager.getConnectionInfo().getIpAddress();

I do not register any receiver to receive any WiFi updates. I use the WifiManager to obtain the IP Address only and nothing else.

Now when I complete login, I call finish() in LoginActivity and start SplashActivity and that's when I see the following log in console:

LoginActivity has leaked IntentReceiver android.net.wifi.WifiManager that was originally registered here. Are you missing a call to unregisterReceiver()?

And I get this log only on marshmallow. This does not happen on <6.0 devices ever. Also on marshmallow, the app never crashes but I see this log every time.

If anyone can explain this behaviour?

camelCaseCoder
  • 1,447
  • 19
  • 32

1 Answers1

2

I had the same error, but only when running in debug for some reason.

Adding: getApplicationContext() fixed the issue for me. (Although I'm not entirely sure why?)

WifiManager wifi_manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

After doing a bit more research it looks like when you transition from one activity to the other the context no longer exists.

getApplicationContext() - Returns the context for all activities running in application.

getContext() - Returns the context view only current running activity.

So it's probably best use getApplicationContext() for anything that should survive for the lifetime of your app.

Community
  • 1
  • 1
Kai
  • 1,709
  • 1
  • 23
  • 36
  • Yes, you're right. I tested this and there was no exception in the logs. An explanation would've been great but I am gonna anyway upvote and accept the answer. Thanks. – camelCaseCoder Nov 15 '16 at 06:37
  • Thanks, I did a bit more research and updated my answer. – Kai Nov 16 '16 at 00:05
  • Yes, it makes sense. But still it's a little bit weird to get leaked `IntentReceiver` for this reason. And also not to forget, this behaviour is only on 6.0+ devices. For <6.0 devices, `getContext()` works just fine. – camelCaseCoder Nov 16 '16 at 04:15