示例代码:
import android.net.ConnectivityManager;
final ConnectivityManager connMgr =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnected() || mobile.isConnected())
return true;else
return false;
ConnectivityManager还有以下一些TYPE
int | TYPE_BLUETOOTH | The Default Bluetooth data connection. |
int | TYPE_DUMMY | Dummy data connection. |
int | TYPE_ETHERNET | The Default Ethernet data connection. |
int | TYPE_MOBILE | The Default Mobile data connection. |
int | TYPE_MOBILE_DUN | A DUN-specific Mobile data connection. |
int | TYPE_MOBILE_HIPRI | A High Priority Mobile data connection. |
int | TYPE_MOBILE_MMS | An MMS-specific Mobile data connection. |
int | TYPE_MOBILE_SUPL | A SUPL-specific Mobile data connection. |
int | TYPE_WIFI | The Default WIFI data connection. |
int | TYPE_WIMAX | The Default WiMAX data connection. |
NetworkInfo还有以下方法
boolean |
isAvailable()
Indicates whether network connectivity is possible.
|
boolean |
isConnected()
Indicates whether network connectivity exists and it is possible to establish connections and pass data.
|
boolean |
isConnectedOrConnecting()
Indicates whether network connectivity exists or is in the process of being established.
|
boolean |
isFailover()
Indicates whether the current attempt to connect to the network resulted from the ConnectivityManager trying to fail over to this network following a disconnect from another network.
|
boolean |
isRoaming()
Indicates whether the device is currently roaming on this network.
|