A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© linxy06 中级黑马   /  2015-12-1 19:22  /  572 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

android 判断网络状态
[java] view plaincopy
  • package com.example.android;  
  •   
  • import java.io.IOException;  
  • import java.net.HttpURLConnection;  
  • import java.net.InetAddress;  
  • import java.net.NetworkInterface;  
  • import java.net.SocketException;  
  • import java.net.URL;  
  • import java.util.Enumeration;  
  •   
  • import android.content.Context;  
  • import android.net.ConnectivityManager;  
  • import android.net.NetworkInfo;  
  • import android.telephony.TelephonyManager;  
  •   
  • public class NetStatus {  
  •   
  •     public static int NET_CNNT_BAIDU_OK = 1; // 正常访问因特网状态  
  •     public static int NET_CNNT_BAIDU_TIMEOUT = 2; // 无法访问因特网状态  
  •     public static int NET_NOT_PREPARE = 3; // 网络未准备好  
  •     public static int NET_ERROR = 4;  
  •     private static int TIMEOUT = 3000;  
  •   
  •     /**
  •      * 返回当前网络状态
  •      *  
  •      * @param context
  •      * @return
  •      */  
  •     public static int getNetState(Context context) {  
  •     try {  
  •         ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
  •         if (connectivity != null) {  
  •         NetworkInfo networkinfo = connectivity.getActiveNetworkInfo();  
  •         if (networkinfo != null) {  
  •             if (networkinfo.isAvailable() && networkinfo.isConnected()) {  
  •             if (!connectionNetwork())  
  •                 return NET_CNNT_BAIDU_TIMEOUT;  
  •             else  
  •                 return NET_CNNT_BAIDU_OK;  
  •             } else {  
  •             return NET_NOT_PREPARE;  
  •             }  
  •         }  
  •         }  
  •     } catch (Exception e) {  
  •     }  
  •     return NET_ERROR;  
  •     }  
  •   
  •     /**
  •      * 拼百度地址
  •      *  
  •      * @return
  •      */  
  •     private static boolean connectionNetwork() {  
  •     boolean result = false;  
  •     HttpURLConnection httpUrl = null;  
  •     try {  
  •         httpUrl = (HttpURLConnection) new URL("http://www.baidu.com").openConnection();  
  •         httpUrl.setConnectTimeout(TIMEOUT);  
  •         httpUrl.connect();  
  •         result = true;  
  •     } catch (IOException e) {  
  •     } finally {  
  •         if (null != httpUrl) {  
  •         httpUrl.disconnect();  
  •         }  
  •         httpUrl = null;  
  •     }  
  •     return result;  
  •     }  
  •   
  •     /**
  •      * 判断当前网络是否是3G网络
  •      *  
  •      * @param context
  •      * @return boolean
  •      */  
  •     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;  
  •     }  
  •   
  •     /**
  •      * 判断当前网络是否是wifi网络
  •      *  
  •      * @param context
  •      * @return boolean
  •      */  
  •     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;  
  •     }  

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马