赞
踩
获取ip地址的时候,如果不注册权限的话,也不会报错,但是不会获取到ip地址(表示这个问题很蛋疼)
获取mac地址和设备号的时候,需要注册权限.不然会报错
所以权限很重要.记得加权限,记得加权限,记得加权限
- /**
- * 2015年12月7日TODOimgod
- */
- package com.bodao.aibang.utils;
-
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- import java.net.SocketException;
- import java.util.Enumeration;
-
- import android.content.Context;
- import android.net.wifi.WifiInfo;
- import android.net.wifi.WifiManager;
- import android.telephony.TelephonyManager;
- import android.util.Log;
-
- /**
- * @author imgod 2015年12月7日
- */
- public class PhoneUtils {
-
- /**
- * 获取手机的ip地址
- * 注意添加权限android:name="android.permission.INTERNET"
- * @return 返回地址是本地地址 例如 192.168.1.100
- */
- 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;
- }
-
- /**
- * 获取本机mac 记得添加权限 android:name="android.permission.ACCESS_WIFI_STATE"
- *
- * @return 返回本机的mac地址
- */
- public static String getLocalMacAddress(Context context) {
- WifiManager wifi = (WifiManager) context.getApplicationContext()
- .getSystemService(Context.WIFI_SERVICE);
- WifiInfo info = wifi.getConnectionInfo();
- return info.getMacAddress();
- }
-
- /**
- * @param ctx
- * 上下文对象 记得添加权限android:name="android.permission.READ_PHONE_STATE"
- * @return 返回获取到的手机信息,返回的东西太多.可以分多个函数进行写
- */
- public static String getPhoneInfo(Context ctx) {
- TelephonyManager tm = (TelephonyManager) ctx.getApplicationContext()
- .getSystemService(Context.TELEPHONY_SERVICE);
- StringBuilder sb = new StringBuilder();
-
- sb.append("\nDeviceID(IMEI)" + tm.getDeviceId());
- sb.append("\nDeviceSoftwareVersion:" + tm.getDeviceSoftwareVersion());
- sb.append("\ngetLine1Number:" + tm.getLine1Number());
- sb.append("\nNetworkCountryIso:" + tm.getNetworkCountryIso());
- sb.append("\nNetworkOperator:" + tm.getNetworkOperator());
- sb.append("\nNetworkOperatorName:" + tm.getNetworkOperatorName());
- sb.append("\nNetworkType:" + tm.getNetworkType());
- sb.append("\nPhoneType:" + tm.getPhoneType());
- sb.append("\nSimCountryIso:" + tm.getSimCountryIso());
- sb.append("\nSimOperator:" + tm.getSimOperator());
- sb.append("\nSimOperatorName:" + tm.getSimOperatorName());
- sb.append("\nSimSerialNumber:" + tm.getSimSerialNumber());
- sb.append("\ngetSimState:" + tm.getSimState());
- sb.append("\nSubscriberId:" + tm.getSubscriberId());
- sb.append("\nVoiceMailNumber:" + tm.getVoiceMailNumber());
- return sb.toString();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。