先是权限,在AndroidManifest.xml添加
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
这是获取所有网络信息的,谢谢写出这代码的大佬
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.util.Log;
import com.example.utils.LogUtil;
public class NetworkUtils {
public static final String WIFI = "Wi-Fi";
public static final String DEFAULT_WIFI_ADDRESS = "00-00-00-00-00-00";
private static final String TAG = "NetworkUtils";
private static ConnectivityManager sConnManager = null;
private static final int[] WEAK_NETWORK_GROUP = new int[]{4, 7, 2, 1};
public NetworkUtils() {
}
public static boolean isConnected(Context context) {
ConnectivityManager connManager = getConnManager(context);
if (connManager != null) {
try {
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
if (networkInfo != null) {
return networkInfo.isConnected();
}
} catch (Exception var3) {
Log.e("NetworkUtils", var3.toString());
}
} else {
Log.e("NetworkUtils", "connManager is null!");
}
return false;
}
public static boolean isConnectedToWeakNetwork(Context context) {
ConnectivityManager connManager = getConnManager(context);
if (connManager != null) {
try {
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
if (networkInfo != null) {
int subType = networkInfo.getSubtype();
LogUtil.i("NetworkUtils", "subType:" + subType + ": name:" + networkInfo.getSubtypeName());
int[] var7;
int var6 = (var7 = WEAK_NETWORK_GROUP).length;
for (int var5 = 0; var5 < var6; ++var5) {
int element = var7[var5];
if (element == subType) {
return true;
}
}
} else {
Log.e("NetworkUtils", "networkInfo is null!");
}
} catch (Exception var8) {
Log.e("NetworkUtils", var8.toString());
}
} else {
Log.e("NetworkUtils", "connManager is null!");
}
return false;
}
public static ConnectivityManager getConnManager(Context context) {
if (context == null) {
Log.e("NetworkUtils", "context is null!");
return null;
} else {
if (sConnManager == null) {
sConnManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
}
return sConnManager;
}
}
public static String[] getNetworkState(Context paramContext) {
String[] arrayOfString = new String[]{"Unknown", "Unknown"};
try {
PackageManager localPackageManager = paramContext.getPackageManager();
if (localPackageManager.checkPermission("android.permission.ACCESS_NETWORK_STATE", paramContext.getPackageName()) != PackageManager.PERMISSION_GRANTED) {
arrayOfString[0] = "Unknown";
return arrayOfString;
}
ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext.getSystemService(Context.CONNECTIVITY_SERVICE);
if (localConnectivityManager == null) {
arrayOfString[0] = "Unknown";
return arrayOfString;
}
NetworkInfo localNetworkInfo1 = localConnectivityManager.getNetworkInfo(1);
if (localNetworkInfo1 != null && localNetworkInfo1.getState() == NetworkInfo.State.CONNECTED) {
arrayOfString[0] = "Wi-Fi";
return arrayOfString;
}
NetworkInfo localNetworkInfo2 = localConnectivityManager.getNetworkInfo(0);
if (localNetworkInfo2 != null && localNetworkInfo2.getState() == NetworkInfo.State.CONNECTED) {
arrayOfString[0] = "2G/3G";
arrayOfString[1] = localNetworkInfo2.getSubtypeName();
return arrayOfString;
}
} catch (Exception var6) {
}
return arrayOfString;
}
public static String getWifiAddress(Context context) {
if (context != null) {
WifiManager wifimanage = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiinfo = wifimanage.getConnectionInfo();
if (wifiinfo != null) {
String address = wifiinfo.getMacAddress();
if (TextUtils.isEmpty(address)){
address = "00-00-00-00-00-00";
}
return address;
} else {
return "00-00-00-00-00-00";
}
} else {
return "00-00-00-00-00-00";
}
}
private static String _convertIntToIp(int i) {
return (i & 255) + "." + (i >> 8 & 255) + "." + (i >> 16 & 255) + "." + (i >> 24 & 255);
}
public static String getWifiIpAddress(Context context) {
if (context != null) {
try {
WifiManager wifiManage = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiinfo = wifiManage.getConnectionInfo();
if (wifiinfo != null) {
return _convertIntToIp(wifiinfo.getIpAddress());
}
return null;
} catch (Exception var3) {
}
}
return null;
}
public static boolean isWifi(Context context) {
if (context != null) {
try {
if (getNetworkState(context)[0].equals("Wi-Fi")) {
return true;
}
} catch (Exception var2) {
}
}
return false;
}
}
当网络状态发生改变的时候,手机会发出广播,所以我们用BroadcastReceiver来接受广播,监听网络,这是网络监听的代码,需要更多信息自行修改NetworkInterface 接口就是
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
public class NetworkHelper {
// TODO: 2019/10/31 通过received监听网络状态变化,通过NetworkUtils了解变化后的网络目前的情况
private Activity activity;
private NetworkInterface anInterface;
public NetworkHelper(Activity activity,NetworkInterface anInterface) {
this.activity = activity;
this.anInterface = anInterface;
receiver = new NetConnectReceiver();
}
private NetConnectReceiver receiver;
private class NetConnectReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
//NetworkUtils有很多方法,这里当初
boolean isContected = NetworkUtils.isConnected(context);
if (anInterface != null) {
anInterface.networkContect(isContected);
}
}
}
}
public void registerReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
activity.registerReceiver(receiver, filter);
}
public void unregisterReceiver() {
activity.unregisterReceiver(receiver);
}
public interface NetworkInterface {
void networkContect(boolean isContect);
}
}
使用方法:在需要网络监听的Activity里新建NetworkHelper ,然后在onStart调用registerReceiver()注册广播,在onDestroy() 调用unregisterReceiver()销毁广播
网友评论