如何在Android手机中检查网络是否连接..(网络意味着没有互联网连接或数据连接其简单的移动网络,如AIRTEL,CELLONE,VODAFONE等..)
我尝试使用ConnectivityManager.TYPE_MOBILE和ConnectivityManager.TYPE_WIFI,但它们用于检查数据连接...而不是网络连接
答案 0 :(得分:0)
您可以使用TelephonyManager's getSimOperator()
答案 1 :(得分:0)
public static boolean isNetworkAvailable(Activity activity) {
ConnectivityManager connectivity = (ConnectivityManager) activity
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null)
{
return false;
} 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;
}
您应该将此权限添加到清单文件
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
答案 2 :(得分:0)
<强> getSimOperator()强>
返回SIM卡提供商的MCC + MNC(移动国家代码+移动网络代码)。
e.g。从SIM卡获取运营商ID(MNC + MCC)
public String getOperator()
{
TelephonyManager manager = (TelephonyManager) mGap.getSystemService(Context.TELEPHONY_SERVICE);
String opeartorName = manager.getSimOperator();
return opeartorName;
}
答案 3 :(得分:0)
试试这个:
public class ConnectionDetector {
private static Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public static boolean isConnectingToInternet(){ boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager)_context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;}
}
在清单文件中添加这些权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
答案 4 :(得分:0)
要检查状态,有两种方法:
通过致电telephonyManager.serviceState.state
得到的信息。
如果您想听更改,可以通过调用telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE)
来使用PhoneStateListener。要取消注册,请致电telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE)
这是一个很好的解决方案,因为即使您没有SIM卡,您似乎有时仍可以(在某些国家/地区)拨打紧急电话号码,例如警察。
enum class PhoneServiceState {
STATE_EMERGENCY_ONLY, STATE_IN_SERVICE, STATE_OUT_OF_SERVICE, STATE_POWER_OFF, UNKNOWN;
companion object {
fun getFromValue(value: Int): PhoneServiceState = when (value) {
ServiceState.STATE_EMERGENCY_ONLY -> STATE_EMERGENCY_ONLY
ServiceState.STATE_IN_SERVICE -> STATE_IN_SERVICE
ServiceState.STATE_OUT_OF_SERVICE -> STATE_OUT_OF_SERVICE
ServiceState.STATE_POWER_OFF -> STATE_POWER_OFF
else -> UNKNOWN
}
}
}
用法:
PhoneServiceState.getFromValue(telephonyManager!!.serviceState.state)
如果是STATE_OUT_OF_SERVICE或STATE_POWER_OFF,则表示您已完全断开连接。如果您拥有STATE_EMERGENCY_ONLY,则意味着您只能拨打紧急电话。
您可以使用subscriptionManager.activeSubscriptionInfoList
。如果您得到一个非空列表,我认为这意味着您已经连接:
fun hasCellConnection(context: Context): Boolean {
val subscriptionManager = context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
val activeSubscriptionInfoList = subscriptionManager.activeSubscriptionInfoList
return !activeSubscriptionInfoList.isNullOrEmpty()
}