解决ConnectivityManager内存泄漏问题

This commit is contained in:
huangjian
2022-06-17 18:29:20 +08:00
parent 50eec199cd
commit 3b2286d916
5 changed files with 41 additions and 343 deletions

View File

@@ -5,6 +5,8 @@ import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import com.yizhuan.xchat_android_library.utils.config.BasicConfig;
public class NetworkUtil {
public static final String TAG = "NetworkUtil";
@@ -15,8 +17,8 @@ public class NetworkUtil {
* @param context
* @return
*/
public static boolean isNetAvailable(Context context) {
NetworkInfo networkInfo = getActiveNetworkInfo(context);
public static boolean isNetAvailable(@Deprecated Context context) {
NetworkInfo networkInfo = getActiveNetworkInfo(BasicConfig.INSTANCE.getAppContext());
if (networkInfo != null) {
return networkInfo.isAvailable();
} else {
@@ -30,9 +32,9 @@ public class NetworkUtil {
* @param context
* @return
*/
private static NetworkInfo getActiveNetworkInfo(Context context) {
private static NetworkInfo getActiveNetworkInfo(@Deprecated Context context) {
try {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) BasicConfig.INSTANCE.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo();
} catch (Exception e) {
return null;
@@ -41,7 +43,7 @@ public class NetworkUtil {
public static boolean getNetworkConnectionStatus(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
ConnectivityManager manager = (ConnectivityManager) BasicConfig.INSTANCE.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (manager == null) {
return false;
}
@@ -51,7 +53,7 @@ public class NetworkUtil {
return false;
}
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
TelephonyManager tm = (TelephonyManager) BasicConfig.INSTANCE.getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
if (tm == null) {
return false;
}