权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.util.StringBuilderPrinter;
import android.widget.TextView;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
/**
* Created by Administrator on 2016/9/23 0023.
*/
public class IpInfoActivity extends AppCompatActivity {
public static String hostip; //本机IP
public static String hostmac; //本机MAC
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
setContentView(tv);
StringBuilder sb = new StringBuilder();
sb.append("网络状态:").append(getCurrentNetType(this)).append("\n");
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info == null) {
sb.append("获取IP 失败");
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
sb.append("WIFI IP地址:").append(getWifiIp());
} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
sb.append("手机 IP地址:").append(getIpAddress());
}
tv.setText(sb.toString());
}
public static String getIpAddress() {
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 instanceof Inet4Address) {
// if (!inetAddress.isLoopbackAddress() && inetAddress
// instanceof Inet6Address) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String getWifiIp() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
// 获得IP地址的方法一:
int ipAddress = info.getIpAddress();
String ipString = "";
if (ipAddress != 0) {
ipString = ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "."
+ (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));
}
return ipString;
}
public String getLocalMacAddress() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
return info.getMacAddress();
}
public static String getCurrentNetType(Context context) {
String type = "";
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info == null) {
type = "null";
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
type = "wifi";
} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
int subType = info.getSubtype();
if (subType == TelephonyManager.NETWORK_TYPE_CDMA || subType == TelephonyManager.NETWORK_TYPE_GPRS
|| subType == TelephonyManager.NETWORK_TYPE_EDGE) {
type = "2g";
} else if (subType == TelephonyManager.NETWORK_TYPE_UMTS || subType == TelephonyManager.NETWORK_TYPE_HSDPA
|| subType == TelephonyManager.NETWORK_TYPE_EVDO_A || subType == TelephonyManager.NETWORK_TYPE_EVDO_0
|| subType == TelephonyManager.NETWORK_TYPE_EVDO_B) {
type = "3g";
} else if (subType == TelephonyManager.NETWORK_TYPE_LTE) {// LTE是3g到4g的过渡,是3.9G的全球标准
type = "4g";
}
}
return type;
}
}
根据网络状态获取Ip地址
最新推荐文章于 2024-07-14 03:28:19 发布