在Android中检查网络连接

3
如何在安卓手机上检查网络是否连接。这里所说的网络并不是互联网连接,而是类似IDEA、AIRTEL这样的移动网络。请帮我解答。

请参考此链接:https://dev59.com/u1jUa4cB1Zd3GeqPP0QK#6357668 - rajeshwaran
4个回答

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" />

0

在链接中,连接管理器是什么?我不明白,请帮帮我,谢谢您的帮助。 - diordna
这基本上是Android的功能,用于检查连接到哪里的是什么/谁 :) http://developer.android.com/reference/org/apache/http/conn/ClientConnectionManager.html 这是它的开发链接 :) - Craig

0

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

我不需要运营商名称,只需要知道网络是否存在。谢谢。 - diordna
@diordna:我明白了,我想你需要找出所有可用的网络提供商名称?那么请查看https://dev59.com/AHA75IYBdhLWcg3wv7_A。 - ρяσѕρєя K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接