在我的android应用程序中,我必须收集大量关于蜂窝塔的信息比如网络名,网络代码等等,但在我开始捕捉这些之前我想确保有手机信号塔连接即GSM或CDMA,但我想确保有连接。否则我只想回去。例如,在Wi - Fi的情况下。在WifiManager的isWifiEnabled()方法的帮助下,我们可以确保我们的Wifi是否在运行。
与此类似,对于Cell Tower
正如这篇文章所描述的,你可以在下面找到
boolean hasNetwork = android.telephony.TelephonyManager.getNetworkType() != android.telephony.TelephonyManager.NETWORK_TYPE_UNKNOWN;
// True if the phone is connected to some type of network i.e. has signal
同一链接的另一种方法,但没有标记为解决方案,是检查网络操作员字段如下
public static Boolean isMobileAvailable(Context appcontext) {
TelephonyManager tel = (TelephonyManager) appcontext.getSystemService(Context.TELEPHONY_SERVICE);
return ((tel.getNetworkOperator() != null && tel.getNetworkOperator().equals("")) ? false : true);
}
试试这个:-
public boolean isNetworkAvailable() {
Context context = getApplicationContext();
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
boitealerte(this.getString(R.string.alert),"getSystemService rend null");
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
如果网络可用,它将返回true