Android获取本机IP地址和MAC地址

先上代码:

package com.example.androidstudy;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//
		Button button_ip = (Button) findViewById(R.id.button_ip);
		Button button_mac = (Button) findViewById(R.id.button_mac);
		//
		button_ip.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					//获取所有本地可用网络接口信息,然后返回枚举中的元素
					for (Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); enumeration.hasMoreElements();) {
						//返回枚举本地网络接口的下一个元素
						NetworkInterface networkInterface = enumeration.nextElement();
						//获取IP地址信息,然后返回枚举中的元素
						for (Enumeration<InetAddress> enumIpAddr = networkInterface.getInetAddresses(); enumIpAddr.hasMoreElements();) {
							//返回枚举集合中的下一个IP地址信息
							InetAddress inetAddress = enumIpAddr.nextElement();
							//如果该地址不为回环地址
							if (!inetAddress.isLoopbackAddress()) {
								//显示出主机IP地址
								Toast.makeText(MainActivity.this, inetAddress.getHostAddress().toString(), Toast.LENGTH_LONG).show();
							}
						}
					}
				} catch (SocketException ex) {
					Log.e("WifiPreference IpAddress", ex.toString());
				}
			}
		});
		button_mac.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//获取wifi系统服务
				WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
				//获取wifi连接信息
				WifiInfo info = wifi.getConnectionInfo();
				//显示出主机MAC地址
				Toast.makeText(MainActivity.this, info.getMacAddress(), Toast.LENGTH_LONG).show();
			}
		});
	}
}

 

@主要用到的类:

Enumeration:

    这是一个公共接口中的列举类,主要是将所需要的信息添加到集合中,方便查找等。此例中就是先后将NetworkInterface和InetAddress信息放在两个不同的集合中。

NetworkInterface:

    这个类用来代表本地设备的网络接口,并提供一些方法来获取本地可用的接口信息。

InetAddress:

    这个类用来获取互联网IP地址,并提供一些方法来获取本机相关信息。

WifiManager:

    这个类用来管理wifi连接。

WifiInfo:

    这个类用来获取当前wifi连接的相关信息。

@最后,需要注意添加权限:

<!-- 访问网络 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 访问Wi-Fi网络 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
@布局文件activity_main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/button_ip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取IP地址" />
     <Button
        android:id="@+id/button_mac"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取MAC地址" />
</LinearLayout>


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值