一、所需權限android
<uses-permission android:name="android.permission.INTERNET"/> //必寫
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>//必寫
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
web
主要函數代碼以下:
// 獲得本機ip地址
public String getLocalHostIp()
{
String ipaddress = "";
try
{
Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces();
// 遍歷所用的網絡接口
while (en.hasMoreElements())
{
NetworkInterface nif = en.nextElement();// 獲得每個網絡接口綁定的全部ip
Enumeration<InetAddress> inet = nif.getInetAddresses();
// 遍歷每個接口綁定的全部ip
while (inet.hasMoreElements())
{
InetAddress ip = inet.nextElement();
if (!ip.isLoopbackAddress()
&& InetAddressUtils.isIPv4Address(ip
.getHostAddress()))
{
return ipaddress = "本機的ip是" + ":" + ip.getHostAddress();
}
}
}
}
catch (SocketException e)
{
Log.e("feige", "獲取本地ip地址失敗");
e.printStackTrace();
}
return ipaddress;
}
// 獲得本機Mac地址
public String getLocalMac()
{
String mac = "";
// 獲取wifi管理器
WifiManager wifiMng = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfor = wifiMng.getConnectionInfo();
mac = "本機的mac地址是:" + wifiInfor.getMacAddress();
return mac;
}網絡