android method to check whether the phone and wireless are connected
- 2020-06-12 10:32:34
- OfStack
This article is an example of android check whether the mobile phone and wireless connection implementation code, to share for your reference. The specific methods are as follows:
Method 1:
The main function codes are as follows:
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if(info!=null && info.isConnected()){
return true;
}else{
return false;
}
Method 2:
The main function codes are as follows:
TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int state = manager.getDataState();
if(state == TelephonyManager.DATA_CONNECTED){
urn true;
}else{
urn false;
}
Method 1 can check the mobile phone connection and the state of wireless connection
Method 2 can only check the connection status of the phone, not the wireless connection status of the phone (i.e., if the phone does not have a phone card, but has a wireless connection, it will return false)
I hope this article has been helpful for your Android programming.