• Android工具类-关于网络、状态的工具类


    下方是一个很好的监测网络、状态的工具类

    1. public class NetworkUtils {  
    2.   
    3.     /** 
    4.      * 网络是否可用 
    5.      *  
    6.      * @param activity 
    7.      * @return 
    8.      */  
    9.     public static boolean isNetworkAvailable(Context context) {  
    10.         ConnectivityManager connectivity = (ConnectivityManager) context  
    11.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
    12.         if (connectivity == null) {  
    13.         } else {  
    14.             NetworkInfo[] info = connectivity.getAllNetworkInfo();  
    15.             if (info != null) {  
    16.                 for (int i = 0; i < info.length; i++) {  
    17.                     if (info[i].getState() == NetworkInfo.State.CONNECTED) {  
    18.                         return true;  
    19.                     }  
    20.                 }  
    21.             }  
    22.         }  
    23.         return false;  
    24.     }  
    25.   
    26.   
    27.     /** 
    28.      * Gps是否打开 
    29.      *  
    30.      * @param context 
    31.      * @return 
    32.      */  
    33.     public static boolean isGpsEnabled(Context context) {  
    34.         LocationManager locationManager = ((LocationManager) context  
    35.                 .getSystemService(Context.LOCATION_SERVICE));  
    36.         List<String> accessibleProviders = locationManager.getProviders(true);  
    37.         return accessibleProviders != null && accessibleProviders.size() > 0;  
    38.     }  
    39.   
    40.     /** 
    41.      * wifi是否打开 
    42.      */  
    43.     public static boolean isWifiEnabled(Context context) {  
    44.         ConnectivityManager mgrConn = (ConnectivityManager) context  
    45.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
    46.         TelephonyManager mgrTel = (TelephonyManager) context  
    47.                 .getSystemService(Context.TELEPHONY_SERVICE);  
    48.         return ((mgrConn.getActiveNetworkInfo() != null && mgrConn  
    49.                 .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel  
    50.                 .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);  
    51.     }  
    52.   
    53.     /** 
    54.      * 判断当前网络是否是wifi网络 
    55.      * if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) {  
    56.      *  
    57.      * @param context 
    58.      * @return boolean 
    59.      */  
    60.     public static boolean isWifi(Context context) {  
    61.         ConnectivityManager connectivityManager = (ConnectivityManager) context  
    62.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
    63.         NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();  
    64.         if (activeNetInfo != null  
    65.                 && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {  
    66.             return true;  
    67.         }  
    68.         return false;  
    69.     }  
    70.   
    71.     /** 
    72.      * 判断当前网络是否3G网络 
    73.      *  
    74.      * @param context 
    75.      * @return boolean 
    76.      */  
    77.     public static boolean is3G(Context context) {  
    78.         ConnectivityManager connectivityManager = (ConnectivityManager) context  
    79.                 .getSystemService(Context.CONNECTIVITY_SERVICE);  
    80.         NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();  
    81.         if (activeNetInfo != null  
    82.                 && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {  
    83.             return true;  
    84.         }  
    85.         return false;  
    86.     }  
    87. }  

    以上方法均通过测试,tv_result为自设定的TextView。

     
      1. tv_result.append("网络是否可用:"+NetworkUtils.isNetworkAvailable(MainActivity.this)+" ");  
      2. tv_result.append("GPS开关是否打开:"+NetworkUtils.isGpsEnabled(MainActivity.this)+" ");  
      3. tv_result.append("是否为3G网络:"+NetworkUtils.is3G(MainActivity.this)+" ");  
      4. tv_result.append("WIFI是否打开:"+NetworkUtils.isWifiEnabled(MainActivity.this)+" ");  
      5. tv_result.append("是否为WIFI网络:"+NetworkUtils.isWifi(MainActivity.this)+" ");  
  • 相关阅读:
    ## 序列化和反序列化
    C#小型资源管理器
    codeforces #310 div1 B
    codeforces #310 div1 A
    BZOJ 1030 文本生成器
    BZOJ 2806 cheat
    BZOJ 2553 禁忌
    BZOJ 4199 品酒大会
    codeforces #309 div1 D
    codeforces #309 div1 C
  • 原文地址:https://www.cnblogs.com/android100/p/android-tools.html
Copyright © 2020-2023  润新知