权限:
<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"/>
package kanfang.qf.com.photeviewviewpager;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
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.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) {
try {
sb.append("WIFI IP地址:").append(getWifiIp()).append("\nMAC地址:").append(getMacAddress());
} catch (SocketException e) {
e.printStackTrace();
}
} 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();
DhcpInfo dhcp = wifi.getDhcpInfo();
String mask = getIpStringByint(dhcp.netmask);
String gateWay = getIpStringByint(dhcp.gateway);
Log.d("ytmfdw", "掩码:" + mask);
Log.d("ytmfdw", "网关:" + gateWay);
// 获得IP地址的方法一:
int ipAddress = info.getIpAddress();
String ipString = "";
ipString = getIpStringByint(ipAddress);
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;
}
public static String getIpStringByint(int ipAddress) {
String ipString = "";
if (ipAddress != 0) {
ipString = ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "."
+ (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));
}
// ipString = Formatter.formatIpAddress(ipAddress);
return ipString;
}
/**
* 6.0以后,获取MAC地址
*
* @return
* @throws SocketException
*/
public static String getMacAddress() throws SocketException {
String address = null;
// 把当前机器上的访问网络接口的存入 Enumeration集合中
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
Log.d("TEST_BUG", " interfaceName = " + interfaces);
while (interfaces.hasMoreElements()) {
NetworkInterface netWork = interfaces.nextElement();
// 如果存在硬件地址并可以使用给定的当前权限访问,则返回该硬件地址(通常是 MAC)。
byte[] by = netWork.getHardwareAddress();
if (by == null || by.length == 0) {
continue;
}
StringBuilder builder = new StringBuilder();
for (byte b : by) {
builder.append(String.format("%02X:", b));
}
if (builder.length() > 0) {
builder.deleteCharAt(builder.length() - 1);
}
String mac = builder.toString();
Log.d("TEST_BUG", "interfaceName=" + netWork.getName() + ", mac=" + mac);
// 从路由器上在线设备的MAC地址列表,可以印证设备Wifi的 name 是 wlan0
if (netWork.getName().equals("wlan0")) {
Log.d("TEST_BUG", " interfaceName =" + netWork.getName() + ", mac=" + mac);
address = mac;
}
}
return address;
}
}