Android之網絡鏈接判斷是否成功

最近工做工程中遇到一個問題。問題很簡單,這裏作個筆記,Android進行網絡聯網的一些操做時,常常會對網絡是否已經鏈接成功進行判斷。咱們一般會對wifi和移動網絡進行判斷,咱們須要判斷網絡設備是否開啓,是否鏈接成功。以前我只是判斷了網絡設備是否已經開啓,而後進行測試的時候,網絡開啓了(我鏈接的是wifi),後臺報錯了,說訪問那個地址沒法訪問的錯誤,後來發現wifi雖然開啓了,可是沒有鏈接成功!因此就得判斷網絡是否鏈接成功!就改寫了代碼,直接判斷網絡是否鏈接。廢話很少說,直接上代碼了。php

複製內容到剪貼板android

代碼:

package com.example.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log;

/**

@author  XuZhiwei (xuzw13@gmail.com)
* Weibo:http://weibo.com/xzw1989
* Create at 2012-9-22 上午11:25:04
*/
public class NetUtil { 
        
        /**
         * 判斷Network是否開啓(包括移動網絡和wifi)
         * 
         * @return
         */
        public static boolean isNetworkEnabled(Context mContext) {
                return ( isNetEnabled(mContext)|| isWIFIEnabled(mContext));
        }
        
        
        /**
         * 判斷Network是否鏈接成功(包括移動網絡和wifi)
         * @return
         */
        public static boolean isNetworkConnected(Context mContext){
                return (isWifiContected(mContext) || isNetContected(mContext));
        }

        /**
         * 判斷移動網絡是否開啓
         * 
         * @return
         */
        public static boolean isNetEnabled(Context context) {
                boolean enable = false;
                TelephonyManager telephonyManager = (TelephonyManager) context
                                .getSystemService(Context.TELEPHONY_SERVICE);
                if (telephonyManager != null) {
                        if (telephonyManager.getNetworkType() != TelephonyManager.NETWORK_TYPE_UNKNOWN) {
                                enable = true;
                                Log.i(Thread.currentThread().getName(), "isNetEnabled");
                        }
                }

                return enable;
        }

        /**
         * 判斷wifi是否開啓
         */
        public static boolean isWIFIEnabled(Context context) {
                boolean enable = false;
                WifiManager wifiManager = (WifiManager) context
                                .getSystemService(Context.WIFI_SERVICE);
                if (wifiManager.isWifiEnabled()) {
                        enable = true;
                        Log.i(Thread.currentThread().getName(), "isWifiEnabled");
                }
                 
                Log.i(Thread.currentThread().getName(), "isWifiDisabled");
                return enable;
        }
    /**
     * 判斷移動網絡是否鏈接成功!
     * @param context
     * @return
     */
        public static boolean isNetContected(Context context){
                ConnectivityManager connectivityManager
                     = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
                 NetworkInfo mobileNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                    if(mobileNetworkInfo.isConnected())
                    {
                            
                            Log.i(Thread.currentThread().getName(), "isNetContected");
                        return true ;
                    }
                        Log.i(Thread.currentThread().getName(), "isNetDisconnected");
                    return false ;

        }
         
        /**
         * 判斷wifi是否鏈接成功
         * @param context
         * @return
         */
        public static boolean isWifiContected(Context context){
                ConnectivityManager connectivityManager
                     = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
                 NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                    if(wifiNetworkInfo.isConnected())
                    {
                            
                            Log.i(Thread.currentThread().getName(), "isWifiContected");
                        return true ;
                    }
                        Log.i(Thread.currentThread().getName(), "isWifiDisconnected");
                    return false ;

        }

}
網絡

    最近工做比較忙,一直都沒時間在這裏發帖,原本想寫個GPS定位過程當中遇到的一些問題的處理與你們交流下經驗的,得等下次咯。
    歡迎你們一塊兒交流學習!請你們多多指教。
    個人博客:http://xuzhiwei.blog.51cto.com
       微博:http://weibo.com/xzw1989學習

相關文章
相關標籤/搜索