Android 常用工具类之DeviceInfoUtil

public class DeviceInfoUtil {
    private static WifiManager wifiManager = null;

    // wifi是否已连接
    public static boolean isWifi(Context context) {
        wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        try {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            if (wifiManager.isWifiEnabled() && wifiInfo.getSSID() != null) {
                return true;
            }
        } catch (Exception e) {
        }
        return false;
    }

    // 获取ipv4地址
    public static String getIpv4(Context context) {
        if (isWifi(context) && wifiManager != null) {
            int ip = wifiManager.getConnectionInfo().getIpAddress();
            return (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "."
                    + ((ip >> 16) & 0xFF) + "." + ((ip >> 24) & 0xFF);
        }
        return null;
    }
}

 

posted @ 2016-04-17 17:00  MarcoReus  阅读(692)  评论(0编辑  收藏  举报