/* * Copyright (C) 2013 WhiteCat 白猫 (www.thinkandroid.cn) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.ta.util.extend.app; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; import com.ta.util.TALogger; import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; /** * @Title IpUtil * @Package com.ta.util.extend.app * @Description IpUtil返回客户端IP地址 * @author 白猫 * @date 2013-1-10 下午 13:53 * @version V1.0 */ public class IpUtil { /** * 使用Wifi时获取IP 设置用户权限 * * <uses-permission * android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> * * <uses-permission * android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> * * <uses-permission * android:name="android.permission.WAKE_LOCK"></uses-permission> * * @return */ public static String getWifiIp(Context context) { // 获取wifi服务 WifiManager wifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); // 判断wifi是否开启 if (!wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(true); } WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); return intToIp(ipAddress); } private static String intToIp(int i) { return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + (i >> 24 & 0xFF); } /** * 使用GPRS上网,时获取ip地址,设置用户上网权限 * * <uses-permission * android:name="android.permission.INTERNET"></uses-permission> * * @return */ public static String getGPRSIp() { 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()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { TALogger.d("IpUtil", ex.getMessage()); } return ""; } }