Android 獲得本地IP地址、外網IP地址、本設備網絡狀態信息、本地Mac地址


本地內網IP和外網IP的區別:

 根據我的經驗一台電腦需要兩個ip才可以上網,一個是本地的內網ip 一個是外網的ip

  本地的ip 一般是192.168.1.2這種樣子  只要在不同的路由器上可以重復

  外網ip 可就不一樣了全世界沒有相同的 可以說每人一個 

 

 ②

 

 

 

一、獲得本地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"/>

主要方法:

 1 public void WifiClick(View v) {
 2         //獲取wifi服務
 3         WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 4         //判斷wifi是否開啟
 5         if (!wifiManager.isWifiEnabled()) {
 6             wifiManager.setWifiEnabled(true);
 7         }
 8         WifiInfo wifiInfo = wifiManager.getConnectionInfo();
 9         int ipAddress = wifiInfo.getIpAddress();
10         String ip = intToIp(ipAddress);
11         WifiIpView.setText("ip:" + ip);
12     }
1 //獲取Wifi ip 地址
2     private String intToIp(int i) {
3         return (i & 0xFF) + "." +
4                 ((i >> 8) & 0xFF) + "." +
5                 ((i >> 16) & 0xFF) + "." +
6                 (i >> 24 & 0xFF);
7     }

 ②移動網絡下

//寫法一 :
1
//獲取本地IP 2 public static String getLocalIpAddress() { 3 try { 4 for (Enumeration<NetworkInterface> en = NetworkInterface 5 .getNetworkInterfaces(); en.hasMoreElements(); ) { 6 NetworkInterface intf = en.nextElement(); 7 for (Enumeration<InetAddress> enumIpAddr = intf 8 .getInetAddresses(); enumIpAddr.hasMoreElements(); ) { 9 InetAddress inetAddress = enumIpAddr.nextElement(); 10 if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) { 11 return inetAddress.getHostAddress().toString(); 12 } 13 } 14 } 15 } catch (SocketException ex) { 16 Log.e("WifiPreference IpAddress", ex.toString()); 17 } 18 return null; 19 }
 1 //寫法二:
 2 /**
 3      * 獲取內網ip地址
 4      * @return
 5      */
 6     public static String getHostIP() {
 7 
 8         String hostIp = null;
 9         try {
10             Enumeration nis = NetworkInterface.getNetworkInterfaces();
11             InetAddress ia = null;
12             while (nis.hasMoreElements()) {
13                 NetworkInterface ni = (NetworkInterface) nis.nextElement();
14                 Enumeration<InetAddress> ias = ni.getInetAddresses();
15                 while (ias.hasMoreElements()) {
16                     ia = ias.nextElement();
17                     if (ia instanceof Inet6Address) {
18                         continue;// skip ipv6
19                     }
20                     String ip = ia.getHostAddress();
21                     if (!"127.0.0.1".equals(ip)) {
22                         hostIp = ia.getHostAddress();
23                         break;
24                     }
25                 }
26             }
27         } catch (SocketException e) {
28             Log.i("yao", "SocketException");
29             e.printStackTrace();
30         }
31         return hostIp;
32     }
 1 //寫法三(推薦):
 2 public String getLocalIpV4Address() {
 3         try {
 4             String ipv4;
 5             ArrayList<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
 6             for (NetworkInterface ni: nilist)
 7             {
 8                 ArrayList<InetAddress>  ialist = Collections.list(ni.getInetAddresses());
 9                 for (InetAddress address: ialist){
10                     if (!address.isLoopbackAddress() && !address.isLinkLocalAddress())
11                     {
12                         ipv4=address.getHostAddress();
13                         return ipv4;
14                     }
15                 }
16 
17             }
18 
19         } catch (SocketException ex) {
20             Log.e("localip", ex.toString());
21         }
22         return null;
23     }

 

二、獲得外網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"/>

方法:

 1 /**
 2      * 獲取外網IP地址
 3      * @return
 4      */
 5     public void GetNetIp() {
 6         new Thread(){
 7             @Override
 8             public void run() {
 9                 String line = "";
10                 URL infoUrl = null;
11                 InputStream inStream = null;
12                 try {
13                     infoUrl = new URL("http://pv.sohu.com/cityjson?ie=utf-8");
14                     URLConnection connection = infoUrl.openConnection();
15                     HttpURLConnection httpConnection = (HttpURLConnection) connection;
16                     int responseCode = httpConnection.getResponseCode();
17                     if (responseCode == HttpURLConnection.HTTP_OK) {
18                         inStream = httpConnection.getInputStream();
19                         BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
20                         StringBuilder strber = new StringBuilder();
21                         while ((line = reader.readLine()) != null)
22                             strber.append(line + "\n");
23                         inStream.close();
24                         // 從反饋的結果中提取出IP地址
25                         int start = strber.indexOf("{");
26                         int end = strber.indexOf("}");
27                         String json = strber.substring(start, end + 1);
28                         if (json != null) {
29                             try {
30                                 JSONObject jsonObject = new JSONObject(json);
31                                 line = jsonObject.optString("cip");
32                             } catch (JSONException e) {
33                                 e.printStackTrace();
34                             }
35                         }
36                         Message msg = new Message();
37                         msg.what = 1;
38                         msg.obj = line;
39                         //向主線程發送消息
40                         handler.sendMessage(msg);
41                     }
42                 } catch (MalformedURLException e) {
43                     e.printStackTrace();
44                 } catch (IOException e) {
45                     e.printStackTrace();
46                 }
47             }
48         }.start();
49     }

注意:主線程不可以進行聯網,拷貝大文件等耗時操作,要在子線程中實現這些操作

三、獲得設備網絡設備狀態信息

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" />

實現方法:

 1 public void AutoClick(View v){
 2         String ip;
 3         ConnectivityManager conMann = (ConnectivityManager)
 4                 this.getSystemService(Context.CONNECTIVITY_SERVICE);
 5         NetworkInfo mobileNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
 6         // 需要<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 7         NetworkInfo wifiNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
 8 
 9         if (mobileNetworkInfo.isConnected()) {
10             ip = getLocalIpV4Address();
11             AutoIpView.setText("IP:" + ip);
12             System.out.println("本地ip-----"+ip);
13         }else if(wifiNetworkInfo.isConnected())
14         {
15             WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
16             WifiInfo wifiInfo = wifiManager.getConnectionInfo();
17             int ipAddress = wifiInfo.getIpAddress();
18             ip = intToIp(ipAddress);
19             AutoIpView.setText("IP:" + ip);
20             System.out.println("wifi_ip地址為------"+ip);
21         }
22     }

四、獲取本地設備Mac地址

mac地址是網卡的唯一標識,通過這個可以判斷網絡當前連接的手機設備有幾台,代碼如下:

 1 public static String getMacAddress(){
 2  /*獲取mac地址有一點需要注意的就是android 6.0版本后,以下注釋方法不再適用,不管任何手機都會返回"02:00:00:00:00:00"這個默認的mac地址,這是googel官方為了加強權限管理而禁用了getSYstemService(Context.WIFI_SERVICE)方法來獲得mac地址。*/
 3         //        String macAddress= "";
 4 //        WifiManager wifiManager = (WifiManager) MyApp.getContext().getSystemService(Context.WIFI_SERVICE);
 5 //        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
 6 //        macAddress = wifiInfo.getMacAddress();
 7 //        return macAddress;
 8 
 9         String macAddress = null;
10         StringBuffer buf = new StringBuffer();
11         NetworkInterface networkInterface = null;
12         try {
13             networkInterface = NetworkInterface.getByName("eth1");
14             if (networkInterface == null) {
15                 networkInterface = NetworkInterface.getByName("wlan0");
16             }
17             if (networkInterface == null) {
18                 return "02:00:00:00:00:02";
19             }
20             byte[] addr = networkInterface.getHardwareAddress();
21             for (byte b : addr) {
22                 buf.append(String.format("%02X:", b));
23             }
24             if (buf.length() > 0) {
25                 buf.deleteCharAt(buf.length() - 1);
26             }
27             macAddress = buf.toString();
28         } catch (SocketException e) {
29             e.printStackTrace();
30             return "02:00:00:00:00:02";
31         }
32         return macAddress;
33     }
34 }

 項目代碼:

 MainActivity.java

 1 package com.example.lifen.myserver;  2 
 3 import android.content.Context;  4 import android.net.ConnectivityManager;  5 import android.net.NetworkInfo;  6 import android.net.wifi.WifiInfo;  7 import android.net.wifi.WifiManager;  8 import android.os.Bundle;  9 import android.os.Handler;  10 import android.os.Message;  11 import android.support.v7.app.AppCompatActivity;  12 import android.util.Log;  13 import android.view.View;  14 import android.widget.TextView;  15 
 16 import org.json.JSONException;  17 import org.json.JSONObject;  18 
 19 import java.io.BufferedReader;  20 import java.io.IOException;  21 import java.io.InputStream;  22 import java.io.InputStreamReader;  23 import java.net.HttpURLConnection;  24 import java.net.Inet6Address;  25 import java.net.InetAddress;  26 import java.net.MalformedURLException;  27 import java.net.NetworkInterface;  28 import java.net.SocketException;  29 import java.net.URL;  30 import java.net.URLConnection;  31 import java.util.ArrayList;  32 import java.util.Collections;  33 import java.util.Enumeration;  34 
 35 public class MainActivity extends AppCompatActivity {  36 
 37     private TextView WifiIpView;  38     private TextView GPRSIpView;  39     private TextView NwIpView;  40     private TextView AutoIpView;  41 
 42     private TextView WwIpView;  43     private TextView MacView;  44 
 45     public Handler handler = new Handler(){  46  @Override  47         public void handleMessage(Message msg) {  48             if(msg.what == 1){  49                 WwIpView.setText("外網IP:" + msg.obj.toString());  50  }  51  }  52  };  53 
 54     public void onCreate(Bundle savedInstanceState) {  55         super.onCreate(savedInstanceState);  56  setContentView(R.layout.activity_main);  57         WifiIpView = (TextView) findViewById(R.id.wifiIp);  58         GPRSIpView = (TextView) findViewById(R.id.gprsIp);  59         NwIpView = (TextView) findViewById(R.id.nwIp);  60         WwIpView = (TextView) findViewById(R.id.wwIp);  61         AutoIpView = (TextView) findViewById(R.id.autoIp);  62         MacView = (TextView) findViewById(R.id.macView);  63  }  64 
 65     public void WifiClick(View v) {  66         //獲取wifi服務
 67         WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);  68         //判斷wifi是否開啟
 69         if (!wifiManager.isWifiEnabled()) {  70             wifiManager.setWifiEnabled(true);  71  }  72         WifiInfo wifiInfo = wifiManager.getConnectionInfo();  73         int ipAddress = wifiInfo.getIpAddress();  74         String ip = intToIp(ipAddress);  75         WifiIpView.setText("ip:" + ip);  76  }  77 
 78     public void GPRSClick(View v){  79         GPRSIpView.setText("ip:" +getLocalIpAddress());  80  }  81 
 82     public void NwClick(View v){  83         NwIpView.setText("內網Ip:" + getHostIP());  84  }  85 
 86     public void WwClick(View v){  87  GetNetIp();  88  }  89 
 90     public void AutoClick(View v){  91  String ip;  92         ConnectivityManager conMann = (ConnectivityManager)  93                 this.getSystemService(Context.CONNECTIVITY_SERVICE);  94         NetworkInfo mobileNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  95         // 需要<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 96         NetworkInfo wifiNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_WIFI);  97 
 98         if (mobileNetworkInfo.isConnected()) {  99             ip = getLocalIpV4Address(); 100             AutoIpView.setText("IP:" + ip); 101             System.out.println("本地ip-----"+ip); 102         }else if(wifiNetworkInfo.isConnected()) 103  { 104             WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 105             WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 106             int ipAddress = wifiInfo.getIpAddress(); 107             ip = intToIp(ipAddress); 108             AutoIpView.setText("IP:" + ip); 109             System.out.println("wifi_ip地址為------"+ip); 110  } 111  } 112 
113     public void MacClick(View v){ 114         MacView.setText("Mac:" + getMacAddress()); 115  } 116 
117     //獲取Wifi ip 地址
118     private String intToIp(int i) { 119         return (i & 0xFF) + "." +
120                 ((i >> 8) & 0xFF) + "." +
121                 ((i >> 16) & 0xFF) + "." +
122                 (i >> 24 & 0xFF); 123  } 124 
125     //獲取本地IP
126     public static String getLocalIpAddress() { 127         try { 128             for (Enumeration<NetworkInterface> en = NetworkInterface 129  .getNetworkInterfaces(); en.hasMoreElements(); ) { 130                 NetworkInterface intf = en.nextElement(); 131                 for (Enumeration<InetAddress> enumIpAddr = intf 132  .getInetAddresses(); enumIpAddr.hasMoreElements(); ) { 133                     InetAddress inetAddress = enumIpAddr.nextElement(); 134                     if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) { 135                         return inetAddress.getHostAddress().toString(); 136  } 137  } 138  } 139         } catch (SocketException ex) { 140             Log.e("WifiPreference IpAddress", ex.toString()); 141  } 142         return null; 143  } 144 
145     /*
146 
147      */
148     public String getLocalIpV4Address() { 149         try { 150  String ipv4; 151             ArrayList<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces()); 152             for (NetworkInterface ni: nilist) 153  { 154                 ArrayList<InetAddress>  ialist = Collections.list(ni.getInetAddresses()); 155                 for (InetAddress address: ialist){ 156                     if (!address.isLoopbackAddress() && !address.isLinkLocalAddress()) 157  { 158                         ipv4=address.getHostAddress(); 159                         return ipv4; 160  } 161  } 162 
163  } 164 
165         } catch (SocketException ex) { 166             Log.e("localip", ex.toString()); 167  } 168         return null; 169  } 170 
171     /**
172  * 獲取內網ip地址 173  * @return
174      */
175     public static String getHostIP() { 176 
177         String hostIp = null; 178         try { 179             Enumeration nis = NetworkInterface.getNetworkInterfaces(); 180             InetAddress ia = null; 181             while (nis.hasMoreElements()) { 182                 NetworkInterface ni = (NetworkInterface) nis.nextElement(); 183                 Enumeration<InetAddress> ias = ni.getInetAddresses(); 184                 while (ias.hasMoreElements()) { 185                     ia = ias.nextElement(); 186                     if (ia instanceof Inet6Address) { 187                         continue;// skip ipv6
188  } 189                     String ip = ia.getHostAddress(); 190                     if (!"127.0.0.1".equals(ip)) { 191                         hostIp = ia.getHostAddress(); 192                         break; 193  } 194  } 195  } 196         } catch (SocketException e) { 197             Log.i("yao", "SocketException"); 198  e.printStackTrace(); 199  } 200         return hostIp; 201  } 202 
203     /**
204  * 獲取外網IP地址 205  * @return
206      */
207     public void GetNetIp() { 208         new Thread(){ 209  @Override 210             public void run() { 211                 String line = ""; 212                 URL infoUrl = null; 213                 InputStream inStream = null; 214                 try { 215                     infoUrl = new URL("http://pv.sohu.com/cityjson?ie=utf-8"); 216                     URLConnection connection = infoUrl.openConnection(); 217                     HttpURLConnection httpConnection = (HttpURLConnection) connection; 218                     int responseCode = httpConnection.getResponseCode(); 219                     if (responseCode == HttpURLConnection.HTTP_OK) { 220                         inStream = httpConnection.getInputStream(); 221                         BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8")); 222                         StringBuilder strber = new StringBuilder(); 223                         while ((line = reader.readLine()) != null) 224                             strber.append(line + "\n"); 225  inStream.close(); 226                         // 從反饋的結果中提取出IP地址
227                         int start = strber.indexOf("{"); 228                         int end = strber.indexOf("}"); 229                         String json = strber.substring(start, end + 1); 230                         if (json != null) { 231                             try { 232                                 JSONObject jsonObject = new JSONObject(json); 233                                 line = jsonObject.optString("cip"); 234                             } catch (JSONException e) { 235  e.printStackTrace(); 236  } 237  } 238                         Message msg = new Message(); 239                         msg.what = 1; 240                         msg.obj = line; 241                         //向主線程發送消息
242  handler.sendMessage(msg); 243  } 244                 } catch (MalformedURLException e) { 245  e.printStackTrace(); 246                 } catch (IOException e) { 247  e.printStackTrace(); 248  } 249  } 250  }.start(); 251  } 252 
253     public static String getMacAddress(){ 254  /*獲取mac地址有一點需要注意的就是android 6.0版本后,以下注釋方法不再適用,不管任何手機都會返回"02:00:00:00:00:00"這個默認的mac地址,這是googel官方為了加強權限管理而禁用了getSYstemService(Context.WIFI_SERVICE)方法來獲得mac地址。*/
255         // String macAddress= ""; 256 // WifiManager wifiManager = (WifiManager) MyApp.getContext().getSystemService(Context.WIFI_SERVICE); 257 // WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 258 // macAddress = wifiInfo.getMacAddress(); 259 // return macAddress;
260 
261         String macAddress = null; 262         StringBuffer buf = new StringBuffer(); 263         NetworkInterface networkInterface = null; 264         try { 265             networkInterface = NetworkInterface.getByName("eth1"); 266             if (networkInterface == null) { 267                 networkInterface = NetworkInterface.getByName("wlan0"); 268  } 269             if (networkInterface == null) { 270                 return "02:00:00:00:00:02"; 271  } 272             byte[] addr = networkInterface.getHardwareAddress(); 273             for (byte b : addr) { 274                 buf.append(String.format("%02X:", b)); 275  } 276             if (buf.length() > 0) { 277                 buf.deleteCharAt(buf.length() - 1); 278  } 279             macAddress = buf.toString(); 280         } catch (SocketException e) { 281  e.printStackTrace(); 282             return "02:00:00:00:00:02"; 283  } 284         return macAddress; 285  } 286 }
View Code

 布局文件:

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3 xmlns:app="http://schemas.android.com/apk/res-auto"
  4 xmlns:tools="http://schemas.android.com/tools"
  5 android:layout_width="match_parent"
  6 android:layout_height="match_parent"
  7 tools:context="com.example.lifen.myserver.MainActivity">
  8 
  9 <ScrollView
 10     android:layout_width="0dp"
 11     android:layout_height="0dp"
 12     tools:layout_constraintTop_creator="1"
 13     tools:layout_constraintRight_creator="1"
 14     tools:layout_constraintBottom_creator="1"
 15     android:layout_marginStart="8dp"
 16     app:layout_constraintBottom_toBottomOf="parent"
 17     android:layout_marginEnd="8dp"
 18     app:layout_constraintRight_toRightOf="parent"
 19     android:layout_marginTop="8dp"
 20     tools:layout_constraintLeft_creator="1"
 21     android:layout_marginBottom="8dp"
 22     app:layout_constraintLeft_toLeftOf="parent"
 23     app:layout_constraintTop_toTopOf="parent"
 24     app:layout_constraintVertical_bias="0.0">
 25     <android.support.constraint.ConstraintLayout
 26         android:layout_width="match_parent"
 27         android:layout_height="wrap_content">
 28 
 29         <Button
 30             android:id="@+id/wifi"
 31             android:onClick="WifiClick"
 32             android:layout_width="wrap_content"
 33             android:layout_height="wrap_content"
 34             android:text="使用WIFI"
 35             tools:layout_constraintTop_creator="1"
 36             android:layout_marginStart="16dp"
 37             android:layout_marginTop="13dp"
 38             app:layout_constraintTop_toBottomOf="@+id/wifiIp"
 39             tools:layout_constraintLeft_creator="1"
 40             app:layout_constraintLeft_toLeftOf="parent" />
 41 
 42         <TextView
 43             android:id="@+id/wifiIp"
 44             android:layout_width="wrap_content"
 45             android:layout_height="wrap_content"
 46             android:text="ip:"
 47             tools:layout_constraintTop_creator="1"
 48             android:layout_marginStart="16dp"
 49             android:layout_marginTop="16dp"
 50             tools:layout_constraintLeft_creator="1"
 51             app:layout_constraintLeft_toLeftOf="parent"
 52             app:layout_constraintTop_toTopOf="parent" />
 53 
 54         <TextView
 55             android:id="@+id/gprsIp"
 56             android:layout_width="wrap_content"
 57             android:layout_height="wrap_content"
 58             android:text="ip:"
 59             tools:layout_constraintTop_creator="1"
 60             android:layout_marginStart="16dp"
 61             android:layout_marginTop="16dp"
 62             app:layout_constraintTop_toBottomOf="@+id/wifi"
 63             tools:layout_constraintLeft_creator="1"
 64             app:layout_constraintLeft_toLeftOf="parent" />
 65 
 66         <Button
 67             android:id="@+id/GPRS"
 68             android:layout_width="wrap_content"
 69             android:layout_height="wrap_content"
 70             android:text="使用GPRS"
 71             android:onClick="GPRSClick"
 72             tools:layout_constraintTop_creator="1"
 73             tools:layout_constraintRight_creator="1"
 74             app:layout_constraintRight_toRightOf="@+id/wifi"
 75             android:layout_marginTop="50dp"
 76             app:layout_constraintTop_toBottomOf="@+id/wifi"
 77             tools:layout_constraintLeft_creator="1"
 78             app:layout_constraintLeft_toLeftOf="@+id/wifi" />
 79 
 80         <TextView
 81             android:id="@+id/nwIp"
 82             android:layout_width="wrap_content"
 83             android:layout_height="wrap_content"
 84             android:text="內網ip:"
 85             tools:layout_constraintBottom_creator="1"
 86             app:layout_constraintBottom_toTopOf="@+id/nw"
 87             android:layout_marginStart="16dp"
 88             tools:layout_constraintLeft_creator="1"
 89             android:layout_marginBottom="17dp"
 90             app:layout_constraintLeft_toLeftOf="parent" />
 91 
 92         <Button
 93             android:id="@+id/nw"
 94             android:onClick="NwClick"
 95             android:layout_width="wrap_content"
 96             android:layout_height="wrap_content"
 97             android:text="內網ip"
 98             tools:layout_constraintTop_creator="1"
 99             tools:layout_constraintRight_creator="1"
100             app:layout_constraintRight_toRightOf="@+id/GPRS"
101             android:layout_marginTop="56dp"
102             app:layout_constraintTop_toBottomOf="@+id/GPRS"
103             tools:layout_constraintLeft_creator="1"
104             app:layout_constraintLeft_toLeftOf="@+id/GPRS" />
105 
106         <TextView
107             android:id="@+id/wwIp"
108             android:layout_width="wrap_content"
109             android:layout_height="wrap_content"
110             android:text="外網Ip:"
111             tools:layout_constraintBottom_creator="1"
112             app:layout_constraintBottom_toTopOf="@+id/ww"
113             android:layout_marginStart="16dp"
114             tools:layout_constraintLeft_creator="1"
115             android:layout_marginBottom="10dp"
116             app:layout_constraintLeft_toLeftOf="parent" />
117 
118         <Button
119             android:id="@+id/ww"
120             android:onClick="WwClick"
121             android:layout_width="wrap_content"
122             android:layout_height="wrap_content"
123             android:text="外網Ip"
124             tools:layout_constraintTop_creator="1"
125             tools:layout_constraintRight_creator="1"
126             app:layout_constraintRight_toRightOf="@+id/nw"
127             android:layout_marginTop="44dp"
128             app:layout_constraintTop_toBottomOf="@+id/nw"
129             tools:layout_constraintLeft_creator="1"
130             app:layout_constraintLeft_toLeftOf="@+id/nw" />
131 
132         <TextView
133             android:id="@+id/autoIp"
134             android:layout_width="wrap_content"
135             android:layout_height="wrap_content"
136             android:text="ip:"
137             tools:layout_constraintTop_creator="1"
138             android:layout_marginStart="16dp"
139             android:layout_marginTop="11dp"
140             app:layout_constraintTop_toBottomOf="@+id/ww"
141             tools:layout_constraintLeft_creator="1"
142             app:layout_constraintLeft_toLeftOf="parent" />
143 
144         <Button
145             android:id="@+id/auto"
146             android:onClick="AutoClick"
147             android:layout_width="wrap_content"
148             android:layout_height="wrap_content"
149             android:text="自動判斷網絡環境獲取Ip"
150             tools:layout_constraintTop_creator="1"
151             tools:layout_constraintRight_creator="1"
152             app:layout_constraintRight_toRightOf="@+id/ww"
153             app:layout_constraintTop_toBottomOf="@+id/autoIp"
154             tools:layout_constraintLeft_creator="1"
155             app:layout_constraintLeft_toRightOf="@+id/ww"
156             app:layout_constraintHorizontal_bias="0.497" />
157 
158         <TextView
159             android:id="@+id/macView"
160             android:text="mac:"
161             android:layout_width="wrap_content"
162             android:layout_height="wrap_content"
163             tools:layout_editor_absoluteX="16dp"
164             android:layout_marginTop="13dp"
165             app:layout_constraintTop_toBottomOf="@+id/auto" />
166         <Button
167             android:id="@+id/mac"
168             android:text="Mac"
169             android:onClick="MacClick"
170             android:layout_width="wrap_content"
171             android:layout_height="wrap_content"
172             android:layout_marginLeft="0dp"
173             app:layout_constraintLeft_toLeftOf="@+id/macView"
174             android:layout_marginTop="9dp"
175             app:layout_constraintTop_toBottomOf="@+id/macView" />
176     </android.support.constraint.ConstraintLayout>
177 </ScrollView>
178 
179 </android.support.constraint.ConstraintLayout>
View Code

 

項目預覽圖:

 

項目下載地址:http://download.csdn.net/download/qq_36726507/10183059

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM