ConnectivityManager application in Android Development

Source: Internet
Author: User

/**
* 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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.