天天看点

Android之网络连接判断

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

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 ([email protected]

 * 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) 

    public static boolean isNetworkConnected(Context mContext){ 

        return (isWifiContected(mContext) || isNetContected(mContext)); 

     * 判断移动网络是否开启 

    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) { 

        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"); 

     * 判断移动网络是否连接成功! 

     * @param context 

    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是否连接成功 

    public static boolean isWifiContected(Context context){ 

         NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

            if(wifiNetworkInfo.isConnected()) 

                Log.i(Thread.currentThread().getName(), "isWifiContected"); 

            Log.i(Thread.currentThread().getName(), "isWifiDisconnected"); 

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

继续阅读