安卓开发 获取手机外网和内网的ip地址
2017-03-01 10:15
239 查看
import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.text.Html; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.Inet4Address; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.NetworkInterface; import java.net.SocketException; import java.net.URL; import java.net.URLConnection; import java.util.Enumeration; /** * Created by sjd on 17-2-28. */ public class IPAddress { //获取外网 public static String GetNetIp() { URL infoUrl = null; InputStream inStream = null; try { infoUrl = new URL("http://1212.ip138.com/ic.asp"); URLConnection connection = infoUrl.openConnection(); HttpURLConnection 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 + "" ); inStream.close(); line = Html.fromHtml(strber.toString()).toString(); // 从反馈的结果中提取出IP地址 line = line.substring(line.indexOf("[") + 1, line.indexOf("]")); return line; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } //获取内网 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) {//当前使用2G/3G/4G网络 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.isLinkLocalAddress() && inetAddress instanceof Inet4Address) { return inetAddress.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络 WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());//得到IPV4地址 return ipAddress; } } else { //当前无网络连接,请在设置中打开网络 } return null; } /** * 将得到的int类型的IP转换为String类型 * * @param ip * @return */ public static String intIP2StringIP(int ip) { return (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." + (ip >> 24 & 0xFF); } }
相关文章推荐
- ios 开发之获取手机的外网IP和内网IP
- iOS 如何获取手机外网IP地址(附内网IP地址)
- iOS 如何获取手机外网IP地址(附内网IP地址)
- 安卓开发获取当前手机应用信息的方法
- 安卓开发 获取手机状态栏的高度
- 安卓开发获取当前手机音量和最大音量
- 安卓获取手机外网ip地址!!
- 安卓开发 局域网UDP获取服务器Ip地址
- 安卓手机获取IP地址
- 安卓开发中轻松获取用户手机号码 两行代码获取用户手机号码
- Android开发,获取手机IP地址的两种方式
- iOS开发之 获取手机的网络的ip地址
- php获取外网和内网的ip地址
- 解决win7使用connectify发射wifi手机显示一直显示获取ip地址等问题
- 通过adb命令获取Android手机的IP地址
- Winform获取内网和外网地址
- Android开发之系统信息——获取Android手机中SD卡内存信息
- C# WinForm 下获取外网的IP地址
- iphone开发 获取手机通话状态