检查android中的网络连接

时间:2012-08-16 09:57:22

标签: android

如何在Android手机中检查网络是否连接..(网络意味着没有网络连接,其简单的移动网络如IDEA,AIRTEL等)请帮帮我

4 个答案:

答案 0 :(得分:0)

好的,你只想查看网络连接? 这种方法似乎相当不错! http://smartandroidians.blogspot.co.uk/2010/03/checking-network-connection-in-android.html 给它一个去,希望它会对它进行排序:)

答案 1 :(得分:0)

您可以使用TelephonyManager检索SIM和网络运营商:

TelephonyManager telephonyManager =((TelephonyManager) 
Context.getSystemService(Context.TELEPHONY_SERVICE));

// Network operator IDEA,AIRTEL...
String networkOperatorName = telephonyManager.getNetworkOperatorName();

// sim operator IDEA,AIRTEL,BSNL,MTNL...
String simOperatorName = telephonyManager.getSimOperatorName();

答案 2 :(得分:0)

检查TelephonyManager课程。 这可能在很多方面帮助你。

答案 3 :(得分:0)

/**
 * Checking whether net connection is available or not.
 * 
 * @param nContext
 * @return true if net connection is avaible otherwise false
 */
public static boolean isNetworkAvailable(Context nContext) {
    boolean isNetAvailable = false;
    if (nContext != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) nContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (mConnectivityManager != null) {
            boolean mobileNetwork = false;
            boolean wifiNetwork = false;
            boolean mobileNetworkConnecetd = false;
            boolean wifiNetworkConnecetd = false;
            NetworkInfo mobileInfo = mConnectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifiInfo = mConnectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (mobileInfo != null)
                mobileNetwork = mobileInfo.isAvailable();
            if (wifiInfo != null)
                wifiNetwork = wifiInfo.isAvailable();
            if (wifiNetwork == true || mobileNetwork == true) {
                if (mobileInfo != null)
                    mobileNetworkConnecetd = mobileInfo
                            .isConnectedOrConnecting();
                wifiNetworkConnecetd = wifiInfo.isConnectedOrConnecting();
            }
            isNetAvailable = (mobileNetworkConnecetd || wifiNetworkConnecetd);
        }
    }
    return isNetAvailable;

}

还在清单权限中添加以下标记:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />