问题描述
因此,我有这种方法可以让我知道用户是否具有有效的Internet连接.它运作良好.但是,泄漏金丝雀已经确定了与ConnectivityManager相关的内存泄漏.目前,我知道在任何时候都不会在代码中的任何位置关闭ConnectivityManager.
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.
我试图在onDestroy中关闭ConnectivityManager.要么这不是一个选择,要么我不知道代码.说实话,我只是试图自动填充以告诉我该怎么做.没有运气.
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();
}
推荐答案
使用它来防止泄漏,
ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
这篇关于ConnectivityManager泄漏,不确定如何解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!