转自:http://my.oschina.net/chenj/blog/68680

近日在鼓捣这玩意,拿出来大家分享一下,代码比较简单,如果有不懂的再问

 <Button
android:id="@+id/btn"
android:text="获取IP"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onclick"
/>
<EditText
android:id="@+id/ip_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/> <EditText
android:id="@+id/mac_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

//这里是权限,添加为AndroidManifest.xml的<manifest>的子节点

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

//接下来就是正文(注意IPV4与IPV6的区别,见注释)

package com.wifi;

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.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
public static String IP; //本机IP
public static String MAC; //本机MAC Button btn=null;
EditText ip_txt=null;
EditText mac_txt=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.btn);
ip_txt=(EditText)findViewById(R.id.ip_txt);
mac_txt=(EditText)findViewById(R.id.mac_txt); }
public void onclick(View v){
switch (v.getId()) {
case R.id.btn:
IP = getLocalIpAddress(); //获取本机IP
MAC = getLocalMacAddress();//获取本机MAC
ip_txt.setText(IP);
mac_txt.setText(MAC);
break;
}
} public 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 instanceof Inet4Address)) { // 这是只获取IPV4的,如果要获取IPV6的,改成instanceof Inet6Address
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("WifiPreference IpAddress", ex.toString());
}
return null;
} public String getLocalMacAddress() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
return info.getMacAddress();
}
}

另一种方法:

WifiManager wifi_service = (WifiManager)getSystemService(WIFI_SERVICE);

//检查wifi是否开启  

if(!wifimanage.isWifiEnabled())  {  

  wifimanage.setWifiEnabled(true);  

}  

WifiInfo wifiinfo= wifimanage.getConnectionInfo();  

String ip=intToIp(wifiinfo.getIpAddress());  

//将获取的int转为真正的ip地址,参考的网上的,修改了下  

private String intToIp(int i)  {
  return (i & 0xFF)+ "." + ((i >> 8 ) & 0xFF)? + "." + ((i >> 16 ) & 0xFF) +"."+((i >> 24 ) & 0xFF );
}

关于WifiManager的用法:http://blog.csdn.net/zd_1471278687/article/details/12158367

android程序获取WIFI的IP地址和MAC地址的更多相关文章

  1. java获取服务器IP地址及MAC地址的方法

    这篇文章主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下   本文实例讲述了 ...

  2. 使用Python获取计算机名,ip地址,mac地址等等

    获取计算机名 # 获取计算机名,常用的方法有三种 import os import socket # method one name = socket.gethostname() print(name ...

  3. Python - 获取本机IP地址、Mac地址

    Python - 获取本机IP地址.Mac地址 在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 直接看代码: # Python - 获取本机I ...

  4. 获取本机的IP地址和mac地址

    1. 以前一直用ipconfig来查看ip地址,哈哈哈,现在发现挺好玩 #获取本机的IP地址和mac地址 import uuid import socket def get_mac_address() ...

  5. IP地址、MAC地址及端口

    概述: IP 是地址,有定位功能(网与网的通讯)     (在逻辑上唯一标识一台电脑)(网络层) MAC 是身份证,无定位功能(在子网干活)(在物理上唯一标识一台电脑) (链路层) 首先是如何查看ip ...

  6. Linux环境下如何配置IP地址、MAC地址

    Linux环境下如何配置IP地址.MAC地址 1.配置IP地址 进入配置IP地址路径,进行修改即可 cd /etc/network vim interface 加入以下内容: iface eth0 i ...

  7. IP地址和MAC地址绑定的必要性

    计算机网络是一个共通的网络,世界上任何计算机都可以互相访问. 实现的原理基于网络通讯的互联网交互五层模型. 计算机网络的历史发展 当计算机网络技术初始利用的时代,几台计算机通过集线器连接,就可以实现网 ...

  8. 转:数据包经由路由转发时源、目的IP地址及MAC地址变化情况

    数据包经由路由转发时源.目的IP地址及MAC地址变化情况.  IP数据包经由路由转发的时候源ip,目的ip,源MAC,目的mac是否发生改变,如何改变?   A—–(B1-B2)—–(C1-C2)—— ...

  9. 为什么同时需要IP地址和MAC地址

    每个以太网设备在出厂时都有一个唯一的MAC地址,为什么还需要为每台主机再分配一个IP地址?或者说每台主机都分配唯一的IP地址,为什么还要在网络设备(如网卡,集线器,路由器等)生产时内嵌一个唯一的MAC ...

随机推荐

  1. python操作sqlite数据库

    root@cacti:~/box# cat convert.py #!/usr/bin/env python import sqlite3,time,rrdtool,os def boxstatus( ...

  2. HSSF,XSSF和SXSSF的区别

    HSSF是POI工程对Excel 97(-2007)文件操作的纯Java实现 XSSF是POI工程对Excel 2007 OOXML (.xlsx)文件操作的纯Java实现 从POI 3.8版本开始, ...

  3. 6、android开发中遇到的bug整理

    1.使用actionProvider时出现的问题 bug复现: 解决方案: //import android.support.v4.view.ActionProvider; import androi ...

  4. Python python 基本语法

    程序1 def buildConnectionString(params): """Build a connection string from a dictionary ...

  5. SqlServer 全局变量

    1.@@ERROR 返与@@ERROR 近语句错误码局限于DML语句select语句执行现错误则返等于0错误码没错则返0通使用判断语句没执行功 -- Create Schema if not one ...

  6. 【BZOJ】【1874】取石子游戏

    SG函数 嗯博弈论入门题,关于SG函数这个东西可以去看VFK神犇的博客,讲的非常清楚Orz. 传送门:vfleaking.blog.163.com/blog/static/17480763420123 ...

  7. Win32 Plus Extra Height of Caption Bar

    you set the size of the non-client area by handling the WM_NCCALCSIZE message. But don't do this unl ...

  8. 用fscanf()从文件取数据时,如何判断文件结束

    例子:从键盘输入若干行字符(每行长度不等),输入后把它们存储到一磁盘文件中.再从该文件中读入这些数据,将其中小写字母转换成大写字母后再显示屏上输出. 有两种方法 1.使用feof()函数 #inclu ...

  9. 使用Visual Studio 2012 开发 Html5 应用

    Visual Studio 一直以来是开发微软旗下应用的利器,只要是开发微软相关的应用无论是Windows程序,WPF,Asp.Net,WinRT Surface,WindowsPhone 等微软旗下 ...

  10. 一套名企WEB前端面试题,不提供答案

    1.说说你对Doctype的理解 2.web产品开发的流程 3.说说你对盒子模型的理解 4.前端页面有哪三层构成,分别是什么?作用是什么? 5.行内元素有哪些?块级元素有哪些?他们如何相互转化? 6. ...