Back to project page android-utils.
The source code is released under:
Apache License
If you think the Android project android-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * Copyright 2014 Zhenguo Jin/*from w ww . j av a 2s . c o m*/ * * 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.worthed.util; import java.util.regex.Matcher; import java.util.regex.Pattern; import android.content.Context; import android.location.LocationManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.NetworkInfo.State; import android.telephony.TelephonyManager; /** * ????? * * @author jingle1267@163.com */ public class NetUtil { /** * ????????????,?????????? * * @param context ??? * @return ???????? */ public static boolean isNetworkAvailable(Context context) { boolean netstate = false; ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { netstate = true; break; } } } } return netstate; } /** * GPS?????? * * @param context ??? * @return Gps???????? */ public static boolean isGpsEnabled(Context context) { LocationManager lm = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); return lm.isProviderEnabled(LocationManager.GPS_PROVIDER); } /** * ????????????????WIFI * * @param context ??? * @return ?????Wifi?? */ public static boolean isWifi(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { return true; } return false; } /** * ????????????????3G * * @param context ??? * @return ?????3G?? */ public static boolean is3G(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) { return true; } return false; } /** * ????????????????4G * * @param context ??? * @return ?????4G?? */ public static boolean is4G(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo != null && activeNetInfo.isConnectedOrConnecting()) { if (activeNetInfo.getType() == TelephonyManager.NETWORK_TYPE_LTE) { return true; } } return false; } /** * ??????WIFI * * @param context ??? * @return ??????Wifi */ public static boolean isWiFi(Context context) { ConnectivityManager manager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) .getState(); if (wifi == State.CONNECTED || wifi == State.CONNECTING) return true; return false; } /** * IP?????? * * @param ip ????????IP???????? * @return ?????IP???? */ public static boolean isIP(String ip) { Pattern pattern = Pattern .compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b"); Matcher matcher = pattern.matcher(ip); return matcher.matches(); } /** * IP????int?? * * @param addr IP???? * @return Integer */ public static int ipToInt(String addr) { String[] addrArray = addr.split("\\."); int num = 0; for (int i = 0; i < addrArray.length; i++) { int power = 3 - i; num += ((Integer.parseInt(addrArray[i]) % 256 * Math .pow(256, power))); } return num; } /** * ??????? NET_NO????? NET_2G:2g?? NET_3G?3g?? NET_4G?4g?? NET_WIFI?wifi * NET_UNKNOWN????? */ public static enum NetState { NET_NO, NET_2G, NET_3G, NET_4G, NET_WIFI, NET_UNKNOWN } ; /** * ????????????? * * @param context ??? * @return ????? */ public NetState isConnected(Context context) { NetState stateCode = NetState.NET_NO; ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni != null && ni.isConnectedOrConnecting()) { switch (ni.getType()) { case ConnectivityManager.TYPE_WIFI: stateCode = NetState.NET_WIFI; break; case ConnectivityManager.TYPE_MOBILE: switch (ni.getSubtype()) { case TelephonyManager.NETWORK_TYPE_GPRS: // ????2g case TelephonyManager.NETWORK_TYPE_CDMA: // ??2g case TelephonyManager.NETWORK_TYPE_EDGE: // ??2g case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: stateCode = NetState.NET_2G; break; case TelephonyManager.NETWORK_TYPE_EVDO_A: // ??3g case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: case TelephonyManager.NETWORK_TYPE_EHRPD: case TelephonyManager.NETWORK_TYPE_HSPAP: stateCode = NetState.NET_3G; break; case TelephonyManager.NETWORK_TYPE_LTE: stateCode = NetState.NET_4G; break; default: stateCode = NetState.NET_UNKNOWN; } break; default: stateCode = NetState.NET_UNKNOWN; } } return stateCode; } }