判断手机是否连接网络
判断设备是否wifi连接
判断设备是否数据连接
判断是否以太网连接
获取设备ip
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import cc.catface.ctool.context.TContext;
public class TNetwork {
/** 判断手机是否连接网络 */
public static boolean isNetAvail() {
ConnectivityManager manager = (ConnectivityManager) TContext.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (null == manager) return false;
NetworkInfo info = manager.getActiveNetworkInfo();
return null != info && info.isAvailable();
}
/* 判断设备是否wifi连接 */
public static boolean isWIFI() {
ConnectivityManager manager = (ConnectivityManager) TContext.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (null == manager) return false;
NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return null != networkInfo && networkInfo.isConnected();
}
/* 判断设备是否数据连接 */
public static boolean isMobile() {
ConnectivityManager manager = (ConnectivityManager) TContext.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (null == manager) return false;
NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_DUN);
return null != networkInfo && networkInfo.isConnected();
}
/* 判断是否以太网连接 */
public static boolean isEthernet() {
ConnectivityManager manager = (ConnectivityManager) TContext.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (null == manager) return false;
NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
return null != networkInfo && networkInfo.isConnected();
}
/** ip */
public static String getIp() {
WifiManager wifiManager = (WifiManager) TContext.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (null == wifiManager) return "";
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
return convertIp(connectionInfo.getIpAddress());
}
private static String convertIp(int ip) {
return (ip & 0xff) + "." + ((ip >> 8) & 0xff) + "." + ((ip >> 16) & 0xff) + "." + ((ip >> 24) & 0xff);
}
}
建议使用方式二,简单好用
网络广播
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
public class NetBroadcastReceiver extends BroadcastReceiver {
private NetEventListener mListener;
@Override public void onReceive(Context context, Intent intent) {
if (null == intent || null == intent.getAction()) {
mListener.onNetChange(NetStateUtil.NETWORK_NONE);
return;
}
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
int netWorkType = NetStateUtil.getNetWorkType();
mListener.onNetChange(netWorkType);
}
}
public interface NetEventListener {
void onNetChange(int netWorkType);
}
public void setListener(NetEventListener listener) {
if (null != listener) this.mListener = listener;
}
}
辅助工具类
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import cc.catface.ctool.context.TContext;
public class NetStateUtil {
public static final int NETWORK_NONE = -1; // 无网络
public static final int NETWORK_MOBILE = 0; // 移动网络
public static final int NETWORK_WIFI = 1; // 无线网络
public static int getNetWorkType() {
int result = NETWORK_NONE;
ConnectivityManager manager = (ConnectivityManager) TContext.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (null == manager) return NETWORK_NONE;
NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
if (null == activeNetworkInfo || !activeNetworkInfo.isConnected()) return result;
if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) result = NETWORK_WIFI;
if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE))
result = NETWORK_MOBILE;
return result;
}
}
``
使用方式
// 初始化广播并通过回调获得网络状态变化的结果
mReceiver = new NetBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mActivity.registerReceiver(mReceiver, filter);
mReceiver.setListener(netWorkType -> {
String netState = "无网络连接";
if (NetStateUtil.NETWORK_WIFI == netWorkType) {
netState = "wifi";
} else if (NetStateUtil.NETWORK_MOBILE == netWorkType) {
netState = "移动网络";
}
mBinding.tiNetState.setContent("当前网络状态:" + netState);
});
// 反注册广播
@Override public void onDestroy() {
super.onDestroy();
if (null != mReceiver) mActivity.unregisterReceiver(mReceiver);
}
自定义LiveData,通过内部的广播结果设置当前LiveData的值
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import androidx.lifecycle.LiveData;
import java.lang.ref.WeakReference;
public class NetworkLiveData extends LiveData<NetworkInfo> {
private final WeakReference<Context> mWeakContext;
private static NetworkLiveData mNetworkLiveData;
private NetworkReceiver mReceiver;
private final IntentFilter mFilter;
private NetworkLiveData(WeakReference<Context> context) {
mWeakContext = context;
mReceiver = new NetworkReceiver();
mFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
}
public static NetworkLiveData getInstance(WeakReference<Context> context) {
if (null == mNetworkLiveData) {
synchronized (NetworkLiveData.class) {
if (null == mNetworkLiveData) {
mNetworkLiveData = new NetworkLiveData(context);
}
}
}
return mNetworkLiveData;
}
@Override protected void onActive() {
super.onActive();
mWeakContext.get().registerReceiver(mReceiver, mFilter);
}
@Override protected void onInactive() {
super.onInactive();
mWeakContext.get().unregisterReceiver(mReceiver);
}
private static class NetworkReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null == manager) return;
NetworkInfo info = manager.getActiveNetworkInfo();
getInstance(new WeakReference<>(context)).setValue(info);
}
}
}
使用方式
NetworkLiveData.getInstance(new WeakReference<>(mActivity)).observe(mActivity, new Observer<NetworkInfo>() {
@Override public void onChanged(NetworkInfo networkInfo) {
TLog.d(getClass().getName() + "-->networkInfo: " + networkInfo);
}
});