<span style="font-size:18px;">/**
* 获取ip
*
* @return
*/
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 instanceof Inet4Address) {
// return inetAddress.getAddress().toString();
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("BaseScanTvDeviceClient", "获取本机IP false =" + ex.toString());
} return null;
} public static String getLocalIPAddress(Context context) {
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ipAddress = FormatIP(wifiInfo.getIpAddress());
return ipAddress;
} public static String FormatIP(int ip) {
return Formatter.formatIpAddress(ip);
} // /获取本机IP地址 public static String getLocalIpAddress(Context ctx) {
WifiManager wifiManager = (WifiManager) ctx
.getSystemService(android.content.Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
try {
return InetAddress.getByName(
String.format("%d.%d.%d.%d", (ipAddress & 0xff),
(ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff),
(ipAddress >> 24 & 0xff))).toString();
} catch (UnknownHostException e) {
return null;
} } // 获取本机的物理地址
public static String getLocalMacAddress(Context ctx) {
WifiManager wifi = (WifiManager) ctx
.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
return info.getMacAddress();
}</span>

android获取ip和本机的物理地址的更多相关文章

  1. Android -- 获取IP和MAC地址

    通过InetAddress.getLocalHost()得到始终是“127.0.0.1”,要想得到真正的网络ip地址要通过下面的方法: 首先新建一个工程,修改AndroidManifest.xml文件 ...

  2. Android获取ip地址

    原文:http://www.cnblogs.com/android100/p/Android-get-ip.html 1.使用WIFI 首先设置用户权限 <uses-permission and ...

  3. Android 获取模拟器与真机数据库

    模拟器: localuser:~ localhost$ adb shell shell@android:/ $ su // 数据库复制到 Download 下 shell@android:/ # cp ...

  4. Android 通用获取Ip的方法(判断手机是否联网的方法)!!!

    大家好,我们这一节讲一下,Android获取Ip的一些方法,在我们开发中,有判断手机是否联网,或者想获得当前手机的Ip地址,当然WIFI连接的和 我们3G卡的Ip地址当然是不一样的. 首先我尝试了如下 ...

  5. android获取本机的IP地址和mac物理地址

    /获取本机IP地址 public String getLocalIpAddress() { WifiManager wifiManager = (WifiManager) getSystemServi ...

  6. Android获取本机IP地址

    一.概述 习惯了Linux下的网络编程,在还没用智能机之前就一直想知道怎么得到手机的IP地址(玩智能机之前我是不搞手机应用的).好了,得知Android是基于Linux内核的,那么不就可以利用之前学的 ...

  7. android 通过socket获取IP

    如题<android 通过socket获取IP>: socket.getInetAddress().getHostAddress();

  8. android获取Mac地址和IP地址

    获取Mac地址实际项目中测试了如下几种方法:(1)设备开通Wifi连接,获取到网卡的MAC地址(但是不开通wifi,这种方法获取不到Mac地址,这种方法也是网络上使用的最多的方法) //根据Wifi信 ...

  9. JAVA从本机获取IP地址

    JAVA从本机获取IP地址 论述: 此篇博客是在工作的时候,需要获得当前网络下面正确的ip地址,在网上查阅很多博客,网上一个比较普遍的说法是通过InetAddress.getLocalHost().g ...

随机推荐

  1. 软件測试系统文章(文件夹&amp;链接在此)

    前言 我会在此账号上写一系列关于软件測试的文章,故在此置顶软件測试系列文章的文件夹和链接,以方便大家阅读! 文件夹 软件測试系列之入门篇(一) 软件測试系列之了解篇(二) 软件測试系列之黑白盒(三) ...

  2. error: png.h not found.

    跑php设备 --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable- ...

  3. [状压dp] hdu 4064 Carcassonne

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4064 Carcassonne Time Limit: 3000/1000 MS (Java/Othe ...

  4. linux下一个C语言flock功能使用 .

    表头文件  #include<sys/file.h> 定义函数  int flock(int fd,int operation); 函数说明  flock()会依參数operation所指 ...

  5. android 编译调用C代码

    博客地址:www.zalezone.cn 前言 需求来源 这几天帮别人做一个简单的androidclient,也没什么功能,主要就是调用C代码来对手机的Wifi网络进行设置.于是也就引出了技术难点所在 ...

  6. python可变交换性能优化

    离许多新的压力python性能优化见交换两个变量值可以使用 a,b = b,a 这样能够提高性能 >>> from timeit import Timer >>> ...

  7. VB6.0“挑衅”.NET!

    来到与两年前接触VB,现在学习VB.NET,这两个看起来真的不得不说,这是相对的似(ps:一分之差,只有三个字母),计等.但他们有又什么不同呢?都说VB.NET高级,比VB究竟高级在哪里了?是不是VB ...

  8. 用MODELLER构建好模型后对loop区域进行自动的优化过程

    一:对生成的模型的所有的loop区域进行优化 # Homology modeling by the automodel class from modeller import * from modell ...

  9. Codeforces 9A-Die Roll(意甲冠军)

    A. Die Roll time limit per test 1 second memory limit per test 64 megabytes input standard input out ...

  10. Andoird实现类似iphone AssistiveTouch的控件的demo

    类似Iphone Assistive Touch的控件的实现 网上也有些这方面的控件,不过貌似不怎么好用,或者是论坛需要积分下载,恰好自己在项目中有用到这种控件,就打算自己写一个,也成功实现了这种功能 ...