Android之网络连接判断

简介:
+关注继续查看

   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.  

 

本文转自xuzw13 51CTO博客,原文链接:http://blog.51cto.com/xuzhiwei/1003059,如需转载请自行联系原作者

相关文章
|
10月前
|
存储 XML 设计模式
一个简单的Android网络访问全局码判断及通用数据解析方案
我们在开发中,网络请求经常会遇到各种错误码的判断。比如下面这样:
91 0
|
传感器 安全 开发工具
Android识别模拟器,判断是模拟器还是真机
Android识别模拟器,判断是模拟器还是真机
1481 0
|
Android开发
Android日期选择器,年月日判断处理。
Android日期选择器,年月日判断处理。
233 0
Android日期选择器,年月日判断处理。
|
XML Java Android开发
如何判断当前屏幕TextView是否会自动换行_Android提高篇(Java)
如何判断当前屏幕TextView是否会自动换行_Android提高篇(Java)
332 0
|
JSON Android开发 图形学
修行Android Studio技巧到出神入化,快速涨薪-【预览列表】、【Null判断】篇
众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣!!!
126 0
|
安全 Java Android开发
【Android 逆向】APK 加壳脱壳现状 | 判断 APK 是否加壳 | APK 逆向流程
【Android 逆向】APK 加壳脱壳现状 | 判断 APK 是否加壳 | APK 逆向流程
957 0
|
Android开发 数据库管理
android sqlite 判断表和表中字段是否存在方法
android sqlite 判断表和表中字段是否存在方法
|
Android开发
android 网络实时监听网络状态变化 及 网络类型判断
android 网络实时监听网络状态变化 及 网络类型判断
|
传感器 Android开发 开发者
Android笔记:判断是否为模拟器(实测夜神通过)
Android笔记:判断是否为模拟器(实测夜神通过)
1353 0
|
Android开发
Android笔记:判断某个字符串里某个字符串的个数
Android笔记:判断某个字符串里某个字符串的个数
313 0
相关产品
云迁移中心
推荐文章
更多