import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.NeighboringCellInfo;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
/**
* 网络检测
*
* @author Jekey Lingz
*
*/
public class NetworkUtil {
Context context;
public NetworkUtil(Context context) {
this.context = context;
}
/**
* Network status
*/
public static final int NO_NETWORK = -1,
/**
* Network status
*/
WIFI_ON = 1,
/**
* Network status
*/
MOBILE_ON = 3,
/**
* Network status
*/
ETH_ON = 5,
/**
* Network status
*/
WIMAX_ON = 6;
public int getCurrentNetStatus() {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null) {
if (ni.isConnected()) {
int t = ni.getType();
if (t == ConnectivityManager.TYPE_MOBILE)
return MOBILE_ON;
if (t == ConnectivityManager.TYPE_WIFI)
return WIFI_ON;
if (t == ConnectivityManager.TYPE_WIMAX)
return WIMAX_ON;
return ETH_ON;
}
}
}
return NO_NETWORK;
}
public int getWifiStrength() {
// Wifi的连接速度及信号强度:
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
// WifiInfo wifiInfo = wifiManager.getConnectionInfo();
WifiInfo info = wifiManager.getConnectionInfo();
if (info.getBSSID() != null) {
// 链接信号强度
int strength = WifiManager.calculateSignalLevel(info.getRssi(), 5);
return strength;
}
return 0;
}
public MyNetworkInfo getNetworkInfo() {
try {
int ns = getCurrentNetStatus();
switch (ns) {
case WIFI_ON:
return getWifiInfo();
case MOBILE_ON:
return getMobileInfo();
case ETH_ON:
return new MyNetworkInfo("eth", "", 5);
default:
return new MyNetworkInfo("off", "", 0);
}
} catch (Exception e) {
// TODO: handle exception
return new MyNetworkInfo("off", "", 0);
}
}
int rssi = 0;
public MyNetworkInfo getMobileInfo() {
// Wifi的连接速度及信号强度:
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
// WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int tdata = tm.getDataState();
int ss = 0;
String t2 = "";
if (tdata == TelephonyManager.DATA_CONNECTED) {
List<NeighboringCellInfo> infos = tm.getNeighboringCellInfo();
// Log.v("AAA",""+infos.size());
tm.listen(new PhoneStateListener() {
@Override
public void onSignalStrengthChanged(int asu) {
// TODO Auto-generated method stub
super.onSignalStrengthChanged(asu);
rssi = asu;
}
}, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
/*
* for (NeighboringCellInfo info : infos) { // 获取邻居小区号 // int
* cid=info.getCid(); // 获取邻居小区LAC,LAC: //
* 位置区域码。为了确定移动台的位置,每个GSM/PLMN的覆盖区都被划分成许多位置区,LAC则用于标识不同的位置区。 //
* info.getLac(); // info.getNetworkType(); // info.getPsc(); //
* 获取邻居小区信号强度 int rssi=info.getRssi(); int s =
* WifiManager.calculateSignalLevel(rssi, 5); Log.v("AAA",
* String.format("{%d,%d}",rssi,s)); if (s > ss) ss = s; }
*/
// 在中国,联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EGDE,电信的2G为CDMA,电信的3G为EVDO
ss = WifiManager.calculateSignalLevel(rssi, 5);
int nt = tm.getNetworkType();
if (nt == TelephonyManager.NETWORK_TYPE_UMTS
|| nt == TelephonyManager.NETWORK_TYPE_HSDPA
|| nt == TelephonyManager.NETWORK_TYPE_EVDO_0
|| nt == TelephonyManager.NETWORK_TYPE_EVDO_A
/* || nt==TelephonyManager.NETWORK_TYPE_EVDO_B */) {
t2 = "3G";
} else {
t2 = "2G";
}
}
MyNetworkInfo res = new MyNetworkInfo("mobile", t2, ss);
return res;
}
public MyNetworkInfo getWifiInfo() {
int ss = 0;
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
// WifiInfo wifiInfo = wifiManager.getConnectionInfo();
WifiInfo info = wifiManager.getConnectionInfo();
if (info.getBSSID() != null) {
// 链接信号强度
ss = WifiManager.calculateSignalLevel(info.getRssi(), 5);
}
return new MyNetworkInfo("wifi", "", ss);
}
public static class MyNetworkInfo {
public String net_type = "", net_type2 = "";
public int strength = 0;
public MyNetworkInfo(String net_type, String net_type2, int strength) {
super();
this.net_type = net_type;
this.net_type2 = net_type2;
this.strength = strength;
}
}
/**
* Get local ip address.
*
* @return
*/
public String getLocalIp() {
//
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null
&& cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
WifiManager wifi = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
int ip = info.getIpAddress();
return (ip & 255) + "." + ((ip >> 8) & 255) + "."
+ ((ip >> 16) & 255) + "." + ((ip >> 24) & 255);
}
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()
&& inetAddress.isSiteLocalAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
}
android 网络检测
最新推荐文章于 2023-12-22 14:05:22 发布