獲取公網(wǎng)IP和局域網(wǎng)IP
獲取公網(wǎng)IP需要進(jìn)行網(wǎng)絡(luò)請(qǐng)求,所以我這里使用線程進(jìn)行訪問(wèn),不然在主線程去修改UI會(huì)報(bào)錯(cuò)
公網(wǎng)IP:
private Handler handler =null;//先定義一個(gè)Handler
handler =new Handler();
new Thread(new Runnable(){
@Override
public void run() {
handler.post(runnableUi);
}
}).start();
Runnable runnableUi = new Runnable() {
@Override
public void run() {
URL infoUrl = null;
InputStream inStream = null;
String ipLine = "";
HttpURLConnection httpConnection = null;
try {
infoUrl = new URL("http://pv.sohu.com/cityjson?ie=utf-8");
URLConnection connection = infoUrl.openConnection();
httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
inStream = httpConnection.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inStream, "utf-8"));
StringBuilder strber = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null){
strber.append(line + "\n");
}
Pattern pattern = Pattern
.compile("((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))");
Matcher matcher = pattern.matcher(strber.toString());
if (matcher.find()) {
ipLine = matcher.group();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inStream.close();
httpConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
String aaaa=ipLine;
gw.setText("公網(wǎng)IP:" + ipLine);
}
};
局域網(wǎng)IP
public static String getIPAddress(Context context) {
NetworkInfo info = ((ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info != null && info.isConnected()) {
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {//當(dāng)前使用2G/3G/4G網(wǎng)絡(luò)
try {
//Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces();
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) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//當(dāng)前使用無(wú)線網(wǎng)絡(luò)
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());//得到IPV4地址
return ipAddress;
}
} else {
//當(dāng)前無(wú)網(wǎng)絡(luò)連接,請(qǐng)?jiān)谠O(shè)置中打開(kāi)網(wǎng)絡(luò)
}
return null;
}
實(shí)測(cè)有效,小米獲取公網(wǎng)IP還有點(diǎn)問(wèn)題,華為沒(méi)問(wèn)題
喜歡的點(diǎn)一個(gè)關(guān)注吧,后續(xù)會(huì)更新這個(gè)簡(jiǎn)書(shū)