So, I have this method that let's me know if the user has an active internet connection. It works well. However, leak canary has identified a memory leak associated with the connectivityManager. I am presently not closing the connectivityManager anywhere in my code at any time that I know of.
I've tried to close the connectivityManager in onDestroy. Either that isn't an option or I don't know the code. Truth be told, I simply tried to get auto fill to tell me how to do it. No luck.
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager =(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo =connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo !=null && activeNetworkInfo.isConnected();
}
Use this to prevent leak,
ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);