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"/>
主要方法:
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 downloadsBATTERY_SERVICE (“batterymanager”)
A BatteryManager for managing battery stateJOB_SCHEDULER_SERVICE (“taskmanager”)
A JobScheduler for managing scheduled tasksNETWORK_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>
项目预览图:

项目下载地址:http://download.csdn.net/download/qq_36726507/10183059
Android 获得本地IP地址、外网IP地址、本设备网络状态信息、本地Mac地址的更多相关文章
- 内网IP和外网IP
1.什么是内网内网就是局域网,比如一个公司的局域网,局域网内每台计算机的IP地址是互异的,但不同局域网内的IP是可以相同的.2.什么是外网外网就是互联网,内网的所有计算机都是连接到一个外网IP,通过外 ...
- 根据Request获取客户端IP 内网IP及外网IP
在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr() ,这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实 ...
- 如何判断自己IP是内网IP还是外网IP
tcp/ip协议中,专门保留了三个IP地址区域作为私有地址,其地址范围如下: 10.0.0.0/8:10.0.0.0-10.255.255.255 172.16.0.0/12:172.16.0.0- ...
- [转]C#取得内网IP、外网IP、客户端IP方法
前言 在 Windows Form Application 里对于取得 IP Address 有内网.外网两种 IP Address ,如果只需要取得内网 IP Address ,可以透过使用 IPH ...
- C# 取得内网IP、外网IP、客户端IP方法
前言 在 Windows Form Application 里对于取得 IP Address 有内网.外网两种 IP Address ,如果只需要取得内网 IP Address ,可以透过使用 IPH ...
- centos 7.0VI命令 和固定内网IP 查看外网IP
VI 命令的使用 直接例子 省的忘记 编辑网卡配置文件 vi /etc/sysconfig/network-scripts/ifcfg-enp2s0 不知道那个网卡 ip addr命令 PS:cent ...
- 为什么WAN口IP和外网IP不一样(不一致)?
正常的网络应该是动态公网ip,也就是路由器里面的WAN口IP与www.ip138.com上面显示的是一致的,不一致的话则说明该网络被电信或者联通做了NAT转发,导致您获取到了一个虚假的IP地址,无法用 ...
- 通过js获取内网ip和外网ip的简单方法 ...
今天遇到了一个需求,需要获取用户当前的内网ip, 找了半天终于找到了方法,遂将找到的方法记录下来,留给需要的人. 1,获取内网ip function getIP(callback) { let rec ...
- 关于session失效的问题(内网IP与外网IP)
参考: 测试环境测试支付宝支付,以ip方式访问,而支付宝支付成功后回调地址配置的是域名形式的.造成支付成功后访问成功页面进入了登录页面 同一个网站,通过域名登录和通过IP登录,所产生的session是 ...
随机推荐
- Unix/Linux进程间通信
一,Linux下进程间通信的几种主要手段简介: 1,管道(Pipe)及有名管道(named pipe) 管道可用于具有亲缘关系进程间的通信 有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功 ...
- VS2012 安装 NPOI (管理NuGet程序包)
问题背景 选择项目后右键==>管理NuGet程序包,搜索NPOI,返回服务器无法找到...404 解决方法: 第一步: 访问:https://www.nuget.org/api/v2/ ...
- 关于mariad&mysql部分
不赘述安装部分 查看安装的mysql版本 连接数据库基本操作: 查看数据库有没有运行: 退出操作:两者的区别就在于有无分号 或者: 查看已经安装的数据库: 新增用户并且赋予权限操作 MariaDB [ ...
- C# 语言历史版本特性(C# 1.0到C# 7.1汇总更新)
历史版本C#作为微软2000年以后.NET平台开发的当家语言,发展至今具有17年的历史,语言本身具有丰富的特性,微软对其更新支持也十分支持.微软将C#提交给标准组织ECMA,C# 5.0目前是ECMA ...
- leetcode300
本题使用回溯法,深度优先搜索.使用隐式条件来进行加速. public class Solution { ; int[] x; Dictionary<int, int> dic = new ...
- python中的多进程与多线程(一)
进程是一个执行中的程序,每个进程有自己的地址空间.内存.数据栈以及其他用于跟踪执行的辅助数据.操作系统管理其上所有进程,并合理分配时间. 进程也可以通过fork或spawn派生新的进程,每个新进程有自 ...
- C++ 50学习 之提高对 C++的认识
转自Effective C++ 理解设计目标. 1.和 C 的兼容性. 2.效率. C++在效率上可以和 C 匹 敌 ---- 二者相差大约在 5%之内. 3.和传统开发工具及环境的兼容性. 4.解决 ...
- vue项目中使用axios上传图片等文件
form表单提交图片会刷新页面,也可以时form绑定到一个隐藏的iframe上,可以实现无刷新提交数据. html代码: <input name="file" type=&q ...
- python 路径处理
1.分解路径名 比如要把xxx/yyy/zzz.py 分解成文件名和目录 两种方法: 一.os.path.split(file) 二.os.path.basename() ; os.path.d ...
- 使用chrome浏览器无法访问github提示不是私密连接且无继续前往选项
在hosts文件中添加如下内容: 192.30.253.112 github.com192.30.253.119 gist.github.com151.101.100.133 assets-cdn.g ...