Android进行网络联网的一些操作时,经常会对网络是否已经连接成功进行判断。我们通常会对wifi和移动网络进行判断,我们需要判断网络设备是否开启,是否连接成功,这里做个笔记哈。

 

            
  1. package com.example.util; 
  2.  
  3. import android.content.Context; 
  4. import android.net.ConnectivityManager; 
  5. import android.net.NetworkInfo; 
  6. import android.net.wifi.WifiManager; 
  7. import android.telephony.TelephonyManager; 
  8. import android.util.Log; 
  9.  
  10. /** 
  11.  *  
  12.  * @author XuZhiwei (xuzw13@gmail.com) 
  13.  * Weibo:http://weibo.com/xzw1989 
  14.  * Create at 2012-9-22 上午11:25:04 
  15.  */ 
  16. public class NetUtil {  
  17.      
  18.     /** 
  19.      * 判断Network是否开启(包括移动网络和wifi) 
  20.      *  
  21.      * @return 
  22.      */ 
  23.     public static boolean isNetworkEnabled(Context mContext) { 
  24.         return ( isNetEnabled(mContext)|| isWIFIEnabled(mContext)); 
  25.     } 
  26.      
  27.      
  28.     /** 
  29.      * 判断Network是否连接成功(包括移动网络和wifi) 
  30.      * @return 
  31.      */ 
  32.     public static boolean isNetworkConnected(Context mContext){ 
  33.         return (isWifiContected(mContext) || isNetContected(mContext)); 
  34.     } 
  35.  
  36.     /** 
  37.      * 判断移动网络是否开启 
  38.      *  
  39.      * @return 
  40.      */ 
  41.     public static boolean isNetEnabled(Context context) { 
  42.         boolean enable = false
  43.         TelephonyManager telephonyManager = (TelephonyManager) context 
  44.                 .getSystemService(Context.TELEPHONY_SERVICE); 
  45.         if (telephonyManager != null) { 
  46.             if (telephonyManager.getNetworkType() != TelephonyManager.NETWORK_TYPE_UNKNOWN) { 
  47.                 enable = true
  48.                 Log.i(Thread.currentThread().getName(), "isNetEnabled"); 
  49.             } 
  50.         } 
  51.  
  52.         return enable; 
  53.     } 
  54.  
  55.     /** 
  56.      * 判断wifi是否开启 
  57.      */ 
  58.     public static boolean isWIFIEnabled(Context context) { 
  59.         boolean enable = false
  60.         WifiManager wifiManager = (WifiManager) context 
  61.                 .getSystemService(Context.WIFI_SERVICE); 
  62.         if (wifiManager.isWifiEnabled()) { 
  63.             enable = true
  64.             Log.i(Thread.currentThread().getName(), "isWifiEnabled"); 
  65.         } 
  66.           
  67.         Log.i(Thread.currentThread().getName(), "isWifiDisabled"); 
  68.         return enable; 
  69.     } 
  70.     /** 
  71.      * 判断移动网络是否连接成功! 
  72.      * @param context 
  73.      * @return 
  74.      */ 
  75.     public static boolean isNetContected(Context context){ 
  76.         ConnectivityManager connectivityManager 
  77.              = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 
  78.          NetworkInfo mobileNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
  79.             if(mobileNetworkInfo.isConnected()) 
  80.             { 
  81.                  
  82.                 Log.i(Thread.currentThread().getName(), "isNetContected"); 
  83.                 return true ; 
  84.             } 
  85.             Log.i(Thread.currentThread().getName(), "isNetDisconnected"); 
  86.             return false ; 
  87.  
  88.     } 
  89.       
  90.     /** 
  91.      * 判断wifi是否连接成功 
  92.      * @param context 
  93.      * @return 
  94.      */ 
  95.     public static boolean isWifiContected(Context context){ 
  96.         ConnectivityManager connectivityManager 
  97.              = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 
  98.          NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
  99.             if(wifiNetworkInfo.isConnected()) 
  100.             { 
  101.                  
  102.                 Log.i(Thread.currentThread().getName(), "isWifiContected"); 
  103.                 return true ; 
  104.             } 
  105.             Log.i(Thread.currentThread().getName(), "isWifiDisconnected"); 
  106.             return false ; 
  107.  
  108.     } 
  109.  

 

更多相关文章

  1. 教你如何开关Android的APN网络
  2. Android中android-async-http开源网络框架的简单使用
  3. Android中的几种网络请求方式详解
  4. Android判断ListView滚动到最顶部第0条item完全完整可见及最底部
  5. Android蓝牙开发浅谈
  6. android网络编程——使用Android中的网络连接
  7. Android利用Fiddler进行网络数据抓包
  8. android 网络开发
  9. Android中的几种网络请求方式详解

随机推荐

  1. Android(安卓)shap selector animation
  2. Android(安卓)资源文件中@、@android:typ
  3. Android世界的15款开源的游戏开发引擎
  4. Android(安卓)组件长宽比重设置总结
  5. 关于Android中xml布局文件之android 入门
  6. Android(安卓)Wear开发者预览配置过程
  7. android 电池(三):android电池系统
  8. android 异步处理
  9. 跟大家分享下Android布局文件layout.xml
  10. Android(安卓)跑马灯效果auto scroll tex