0

I wrote myself a little helper class:

public enum NetworkUtils {
    ;

    public static boolean hasNetworkConnection(Context context) {
        ConnectivityManager manager = getConnectivityManager(context);
        return manager != null
                && manager.getActiveNetworkInfo() != null
                && manager.getActiveNetworkInfo().isConnectedOrConnecting();
    }

    private static ConnectivityManager getConnectivityManager(Context context) {
        return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    }
}

LeakCanary keeps telling me that I have an activity leak due to a reference to ConnectivityManager when I use that.

How to handle this situation? Is this a false positive?

JDC
  • 4,247
  • 5
  • 31
  • 74

1 Answers1

0

Found the problem myself: Leak Canary Github issue
The error can be prevented by using the application context:

(ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
JDC
  • 4,247
  • 5
  • 31
  • 74