如何在安卓手机上检查网络是否连接。这里所说的网络并不是互联网连接,而是类似IDEA、AIRTEL这样的移动网络。请帮我解答。
/**
* 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" />
好的,你只是想检查网络连接吗?这个方法看起来非常适合! http://smartandroidians.blogspot.co.uk/2010/03/checking-network-connection-in-android.html 试试看,希望可以解决问题 :)
您可以使用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();