Android 获得本地IP地址、外网IP地址、本设备网络状态信息、本地Mac地址

时间:2021-10-24 08:07:15

本地内网IP和外网IP的区别:

根据我的经验一台电脑需要两个ip才可以上网,一个是本地的内网ip 一个是外网的ip

本地的ip 一般是192.168.1.2这种样子  只要在不同的路由器上可以重复

外网ip 可就不一样了全世界没有相同的 可以说每人一个

Android 获得本地IP地址、外网IP地址、本设备网络状态信息、本地Mac地址

Android 获得本地IP地址、外网IP地址、本设备网络状态信息、本地Mac地址

Android 获得本地IP地址、外网IP地址、本设备网络状态信息、本地Mac地址

Android 获得本地IP地址、外网IP地址、本设备网络状态信息、本地Mac地址

Android 获得本地IP地址、外网IP地址、本设备网络状态信息、本地Mac地址

一、获得本地IP地址

获得本地IP地址有两种情况:一是wifi下,二是移动网络下

①wifi下

需要添加的权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>

主要方法:

 public void WifiClick(View v) {
//获取wifi服务
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
//判断wifi是否开启
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = intToIp(ipAddress);
WifiIpView.setText("ip:" + ip);
}
 //获取Wifi ip 地址
private String intToIp(int i) {
return (i & 0xFF) + "." +
((i >> 8) & 0xFF) + "." +
((i >> 16) & 0xFF) + "." +
(i >> 24 & 0xFF);
}

 ②移动网络下

//写法一 :
1 //获取本地IP
public static String getLocalIpAddress() {
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()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("WifiPreference IpAddress", ex.toString());
}
return null;
}
 //写法二:
/**
* 获取内网ip地址
* @return
*/
public static String getHostIP() { String hostIp = null;
try {
Enumeration nis = NetworkInterface.getNetworkInterfaces();
InetAddress ia = null;
while (nis.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) nis.nextElement();
Enumeration<InetAddress> ias = ni.getInetAddresses();
while (ias.hasMoreElements()) {
ia = ias.nextElement();
if (ia instanceof Inet6Address) {
continue;// skip ipv6
}
String ip = ia.getHostAddress();
if (!"127.0.0.1".equals(ip)) {
hostIp = ia.getHostAddress();
break;
}
}
}
} catch (SocketException e) {
Log.i("yao", "SocketException");
e.printStackTrace();
}
return hostIp;
}
 //写法三(推荐):
public String getLocalIpV4Address() {
try {
String ipv4;
ArrayList<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface ni: nilist)
{
ArrayList<InetAddress> ialist = Collections.list(ni.getInetAddresses());
for (InetAddress address: ialist){
if (!address.isLoopbackAddress() && !address.isLinkLocalAddress())
{
ipv4=address.getHostAddress();
return ipv4;
}
} } } catch (SocketException ex) {
Log.e("localip", ex.toString());
}
return null;
}

二、获得外网IP地址

权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>

方法:

 /**
* 获取外网IP地址
* @return
*/
public void GetNetIp() {
new Thread(){
@Override
public void run() {
String line = "";
URL infoUrl = null;
InputStream inStream = null;
try {
infoUrl = new URL("http://pv.sohu.com/cityjson?ie=utf-8");
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();
while ((line = reader.readLine()) != null)
strber.append(line + "\n");
inStream.close();
// 从反馈的结果中提取出IP地址
int start = strber.indexOf("{");
int end = strber.indexOf("}");
String json = strber.substring(start, end + 1);
if (json != null) {
try {
JSONObject jsonObject = new JSONObject(json);
line = jsonObject.optString("cip");
} catch (JSONException e) {
e.printStackTrace();
}
}
Message msg = new Message();
msg.what = 1;
msg.obj = line;
//向主线程发送消息
handler.sendMessage(msg);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}

注意:主线程不可以进行联网,拷贝大文件等耗时操作,要在子线程中实现这些操作

三、获得设备网络设备状态信息

Context.getSystemService()这个方法是非常实用的方法,只须在参数里输入一个String 字符串常量就可得到对应的服务管理方法,可以用来获取绝大部分的系统信息,各个常量对应的含义如下:

    • WINDOW_SERVICE (“window”) 
      The top-level window manager in which you can place custom windows. The returned object is a WindowManager.

    • LAYOUT_INFLATER_SERVICE (“layout_inflater”) 
      A LayoutInflater for inflating layout resources in this context.

    • ACTIVITY_SERVICE (“activity”) 
      A ActivityManager for interacting with the global activity state of the system.

    • POWER_SERVICE (“power”) 
      A PowerManager for controlling power management.

    • ALARM_SERVICE (“alarm”) 
      A AlarmManager for receiving intents at the time of your choosing.
    • NOTIFICATION_SERVICE (“notification”) 
      A NotificationManager for informing the user of background events.

    • KEYGUARD_SERVICE (“keyguard”) 
      A KeyguardManager for controlling keyguard.

    • LOCATION_SERVICE (“location”) 
      A LocationManager for controlling location (e.g., GPS) updates.

    • SEARCH_SERVICE (“search”) 
      A SearchManager for handling search.

    • VIBRATOR_SERVICE (“vibrator”) 
      A Vibrator for interacting with the vibrator hardware.

    • CONNECTIVITY_SERVICE (“connection”) 
      A ConnectivityManager for handling management of network connections.

    • WIFI_SERVICE (“wifi”) 
      A WifiManager for management of Wi-Fi connectivity.

    • WIFI_P2P_SERVICE (“wifip2p”) 
      A WifiP2pManager for management of Wi-Fi Direct connectivity.

    • INPUT_METHOD_SERVICE (“input_method”) 
      An InputMethodManager for management of input methods.

    • UI_MODE_SERVICE (“uimode”) 
      An UiModeManager for controlling UI modes.

    • DOWNLOAD_SERVICE (“download”) 
      A DownloadManager for requesting HTTP downloads

    • BATTERY_SERVICE (“batterymanager”) 
      A BatteryManager for managing battery state

    • JOB_SCHEDULER_SERVICE (“taskmanager”) 
      A JobScheduler for managing scheduled tasks

    • NETWORK_STATS_SERVICE (“netstats”) 
      A NetworkStatsManager for querying network usage statistics. 
      Note: System services obtained via this API may be closely associated with the Context in which they are obtained from. In general, do not share the service objects between various different contexts (Activities, Applications, Services, Providers, etc.)


    • Parameters 
      name 
      The name of the desired service.

    • Returns 
      The service or null if the name does not exist

要获取IP地址需要用到Context.CONNECTIVITY_SERVICE,这个常量所对应的网络连接的管理方法。代码如下

需要添加权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

实现方法:

 public void AutoClick(View v){
String ip;
ConnectivityManager conMann = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobileNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// 需要<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
NetworkInfo wifiNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mobileNetworkInfo.isConnected()) {
ip = getLocalIpV4Address();
AutoIpView.setText("IP:" + ip);
System.out.println("本地ip-----"+ip);
}else if(wifiNetworkInfo.isConnected())
{
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
ip = intToIp(ipAddress);
AutoIpView.setText("IP:" + ip);
System.out.println("wifi_ip地址为------"+ip);
}
}

四、获取本地设备Mac地址

mac地址是网卡的唯一标识,通过这个可以判断网络当前连接的手机设备有几台,代码如下:

 public static String getMacAddress(){
/*获取mac地址有一点需要注意的就是android 6.0版本后,以下注释方法不再适用,不管任何手机都会返回"02:00:00:00:00:00"这个默认的mac地址,这是googel官方为了加强权限管理而禁用了getSYstemService(Context.WIFI_SERVICE)方法来获得mac地址。*/
// String macAddress= "";
// WifiManager wifiManager = (WifiManager) MyApp.getContext().getSystemService(Context.WIFI_SERVICE);
// WifiInfo wifiInfo = wifiManager.getConnectionInfo();
// macAddress = wifiInfo.getMacAddress();
// return macAddress; String macAddress = null;
StringBuffer buf = new StringBuffer();
NetworkInterface networkInterface = null;
try {
networkInterface = NetworkInterface.getByName("eth1");
if (networkInterface == null) {
networkInterface = NetworkInterface.getByName("wlan0");
}
if (networkInterface == null) {
return "02:00:00:00:00:02";
}
byte[] addr = networkInterface.getHardwareAddress();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
macAddress = buf.toString();
} catch (SocketException e) {
e.printStackTrace();
return "02:00:00:00:00:02";
}
return macAddress;
}
}

项目代码:

MainActivity.java

 package com.example.lifen.myserver;

 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.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView; import org.json.JSONException;
import org.json.JSONObject; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Inet6Address;
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.ArrayList;
import java.util.Collections;
import java.util.Enumeration; public class MainActivity extends AppCompatActivity { private TextView WifiIpView;
private TextView GPRSIpView;
private TextView NwIpView;
private TextView AutoIpView; private TextView WwIpView;
private TextView MacView; public Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what == 1){
WwIpView.setText("外网IP:" + msg.obj.toString());
}
}
}; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WifiIpView = (TextView) findViewById(R.id.wifiIp);
GPRSIpView = (TextView) findViewById(R.id.gprsIp);
NwIpView = (TextView) findViewById(R.id.nwIp);
WwIpView = (TextView) findViewById(R.id.wwIp);
AutoIpView = (TextView) findViewById(R.id.autoIp);
MacView = (TextView) findViewById(R.id.macView);
} public void WifiClick(View v) {
//获取wifi服务
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
//判断wifi是否开启
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = intToIp(ipAddress);
WifiIpView.setText("ip:" + ip);
} public void GPRSClick(View v){
GPRSIpView.setText("ip:" +getLocalIpAddress());
} public void NwClick(View v){
NwIpView.setText("内网Ip:" + getHostIP());
} public void WwClick(View v){
GetNetIp();
} public void AutoClick(View v){
String ip;
ConnectivityManager conMann = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobileNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// 需要<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
NetworkInfo wifiNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mobileNetworkInfo.isConnected()) {
ip = getLocalIpV4Address();
AutoIpView.setText("IP:" + ip);
System.out.println("本地ip-----"+ip);
}else if(wifiNetworkInfo.isConnected())
{
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
ip = intToIp(ipAddress);
AutoIpView.setText("IP:" + ip);
System.out.println("wifi_ip地址为------"+ip);
}
} public void MacClick(View v){
MacView.setText("Mac:" + getMacAddress());
} //获取Wifi ip 地址
private String intToIp(int i) {
return (i & 0xFF) + "." +
((i >> 8) & 0xFF) + "." +
((i >> 16) & 0xFF) + "." +
(i >> 24 & 0xFF);
} //获取本地IP
public static String getLocalIpAddress() {
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()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("WifiPreference IpAddress", ex.toString());
}
return null;
} /* */
public String getLocalIpV4Address() {
try {
String ipv4;
ArrayList<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface ni: nilist)
{
ArrayList<InetAddress> ialist = Collections.list(ni.getInetAddresses());
for (InetAddress address: ialist){
if (!address.isLoopbackAddress() && !address.isLinkLocalAddress())
{
ipv4=address.getHostAddress();
return ipv4;
}
} } } catch (SocketException ex) {
Log.e("localip", ex.toString());
}
return null;
} /**
* 获取内网ip地址
* @return
*/
public static String getHostIP() { String hostIp = null;
try {
Enumeration nis = NetworkInterface.getNetworkInterfaces();
InetAddress ia = null;
while (nis.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) nis.nextElement();
Enumeration<InetAddress> ias = ni.getInetAddresses();
while (ias.hasMoreElements()) {
ia = ias.nextElement();
if (ia instanceof Inet6Address) {
continue;// skip ipv6
}
String ip = ia.getHostAddress();
if (!"127.0.0.1".equals(ip)) {
hostIp = ia.getHostAddress();
break;
}
}
}
} catch (SocketException e) {
Log.i("yao", "SocketException");
e.printStackTrace();
}
return hostIp;
} /**
* 获取外网IP地址
* @return
*/
public void GetNetIp() {
new Thread(){
@Override
public void run() {
String line = "";
URL infoUrl = null;
InputStream inStream = null;
try {
infoUrl = new URL("http://pv.sohu.com/cityjson?ie=utf-8");
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();
while ((line = reader.readLine()) != null)
strber.append(line + "\n");
inStream.close();
// 从反馈的结果中提取出IP地址
int start = strber.indexOf("{");
int end = strber.indexOf("}");
String json = strber.substring(start, end + 1);
if (json != null) {
try {
JSONObject jsonObject = new JSONObject(json);
line = jsonObject.optString("cip");
} catch (JSONException e) {
e.printStackTrace();
}
}
Message msg = new Message();
msg.what = 1;
msg.obj = line;
//向主线程发送消息
handler.sendMessage(msg);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
} public static String getMacAddress(){
/*获取mac地址有一点需要注意的就是android 6.0版本后,以下注释方法不再适用,不管任何手机都会返回"02:00:00:00:00:00"这个默认的mac地址,这是googel官方为了加强权限管理而禁用了getSYstemService(Context.WIFI_SERVICE)方法来获得mac地址。*/
// String macAddress= "";
// WifiManager wifiManager = (WifiManager) MyApp.getContext().getSystemService(Context.WIFI_SERVICE);
// WifiInfo wifiInfo = wifiManager.getConnectionInfo();
// macAddress = wifiInfo.getMacAddress();
// return macAddress; String macAddress = null;
StringBuffer buf = new StringBuffer();
NetworkInterface networkInterface = null;
try {
networkInterface = NetworkInterface.getByName("eth1");
if (networkInterface == null) {
networkInterface = NetworkInterface.getByName("wlan0");
}
if (networkInterface == null) {
return "02:00:00:00:00:02";
}
byte[] addr = networkInterface.getHardwareAddress();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
macAddress = buf.toString();
} catch (SocketException e) {
e.printStackTrace();
return "02:00:00:00:00:02";
}
return macAddress;
}
}

布局文件:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lifen.myserver.MainActivity"> <ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <Button
android:id="@+id/wifi"
android:onClick="WifiClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用WIFI"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="16dp"
android:layout_marginTop="13dp"
app:layout_constraintTop_toBottomOf="@+id/wifiIp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent" /> <TextView
android:id="@+id/wifiIp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ip:"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <TextView
android:id="@+id/gprsIp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ip:"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/wifi"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent" /> <Button
android:id="@+id/GPRS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用GPRS"
android:onClick="GPRSClick"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/wifi"
android:layout_marginTop="50dp"
app:layout_constraintTop_toBottomOf="@+id/wifi"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/wifi" /> <TextView
android:id="@+id/nwIp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内网ip:"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/nw"
android:layout_marginStart="16dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="17dp"
app:layout_constraintLeft_toLeftOf="parent" /> <Button
android:id="@+id/nw"
android:onClick="NwClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内网ip"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/GPRS"
android:layout_marginTop="56dp"
app:layout_constraintTop_toBottomOf="@+id/GPRS"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/GPRS" /> <TextView
android:id="@+id/wwIp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="外网Ip:"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/ww"
android:layout_marginStart="16dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="10dp"
app:layout_constraintLeft_toLeftOf="parent" /> <Button
android:id="@+id/ww"
android:onClick="WwClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="外网Ip"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/nw"
android:layout_marginTop="44dp"
app:layout_constraintTop_toBottomOf="@+id/nw"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/nw" /> <TextView
android:id="@+id/autoIp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ip:"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="16dp"
android:layout_marginTop="11dp"
app:layout_constraintTop_toBottomOf="@+id/ww"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent" /> <Button
android:id="@+id/auto"
android:onClick="AutoClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动判断网络环境获取Ip"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/ww"
app:layout_constraintTop_toBottomOf="@+id/autoIp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toRightOf="@+id/ww"
app:layout_constraintHorizontal_bias="0.497" /> <TextView
android:id="@+id/macView"
android:text="mac:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="16dp"
android:layout_marginTop="13dp"
app:layout_constraintTop_toBottomOf="@+id/auto" />
<Button
android:id="@+id/mac"
android:text="Mac"
android:onClick="MacClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="@+id/macView"
android:layout_marginTop="9dp"
app:layout_constraintTop_toBottomOf="@+id/macView" />
</android.support.constraint.ConstraintLayout>
</ScrollView> </android.support.constraint.ConstraintLayout>

项目预览图:

Android 获得本地IP地址、外网IP地址、本设备网络状态信息、本地Mac地址

项目下载地址:http://download.csdn.net/download/qq_36726507/10183059