/**
* Test the main management and network connection operations of ConnectivityManager.
* TelephonyManager manages information related to mobile phones and carriers, and WifiManager manages information related to wifi.
* To access the network status, you must first add the permission <uses-permission
* Android: name = "android. permission. ACCESS_NETWORK_STATE"/>
* The NetworkInfo class contains a detailed description of the connection between Wi-Fi and mobile network modes. The State object obtained through its getState () method represents
* The connection is successful or not.
*
*/
Public void testConnectivityManager ()
{
ConnectivityManager connManager = (ConnectivityManager) this
. GetSystemService (CONNECTIVITY_SERVICE );
// Obtain the NetWorkInfo object that represents the networking status
NetworkInfo networkInfo = connManager. getActiveNetworkInfo ();
// Obtain whether the current network connection is available
If (null = networkInfo)
{
Toast. makeText (this, "the current network connection is unavailable", Toast. LENGTH_SHORT). show ();
// When the network is unavailable, the network settings page is displayed.
StartActivityForResult (new Intent (
Android. provider. Settings. ACTION_WIRELESS_SETTINGS), 1 );
} Else
{
Boolean available = networkInfo. isAvailable ();
If (available)
{
Log. I ("notification", "current network connection available ");
Toast. makeText (this, "the current network connection is available", Toast. LENGTH_SHORT). show ();
} Else
{
Log. I ("notification", "the current network connection is unavailable ");
Toast. makeText (this, "the current network connection is unavailable", Toast. LENGTH_SHORT). show ();
}
}
State state = connManager. getNetworkInfo (
ConnectivityManager. TYPE_MOBILE). getState ();
If (State. CONNECTED = state)
{
Log. I ("notification", "GPRS network connected ");
Toast. makeText (this, "GPRS network connected", Toast. LENGTH_SHORT). show ();
}
State = connManager. getNetworkInfo (ConnectivityManager. TYPE_WIFI)
. GetState ();
If (State. CONNECTED = state)
{
Log. I ("notification", "WIFI network connected ");
Toast. makeText (this, "WIFI network connected", Toast. LENGTH_SHORT). show ();
}
/// Jump to the wireless network settings page
// StartActivity (new
// Intent (android. provider. Settings. ACTION_WIRELESS_SETTINGS ));
/// Jump to the unlimited wifi network settings page
// StartActivity (new
// Intent (android. provider. Settings. ACTION_WIFI_SETTINGS ));
/**
* Check network connection conditions
*
* @ Return 0: None, 1: Wifi, 2: GPRS, 3: Other
*/
Protected int checkNetworkType (){
// If (Global. IsDebug)
// Return 1;
ConnectivityManager connManager = (ConnectivityManager) context
. GetSystemService (InitActivity. CONNECTIVITY_SERVICE );
// Obtain the NetWorkInfo object that represents the networking status
NetworkInfo networkInfo = connManager. getActiveNetworkInfo ();
// Obtain whether the current network connection is available
If (networkInfo = null |! NetworkInfo. isAvailable ())
Return 0;
// Wifi
State state = connManager. getNetworkInfo (ConnectivityManager. TYPE_WIFI). getState ();
If (State. CONNECTED = state ){
Return 1;
}
// GPRS
State = connManager. getNetworkInfo (ConnectivityManager. TYPE_MOBILE). getState ();
If (State. CONNECTED = state ){
Return 2;
}
Return 3;
}