Network: Android network judgment (wifi, 3G, and others)

Source: Internet
Author: User

Network: Android network judgment (wifi, 3G, and others)

Public class NetworkProber {

/**
* Whether the Network is available
*
* @ Param activity
* @ Return
*/
Public static boolean isNetworkAvailable (Context context ){
ConnectivityManager connectivity = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE );
If (connectivity = null ){
} 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;
}

/**
* Whether or not Gps is enabled
*
* @ Param context
* @ Return
*/
Public static boolean isGpsEnabled (Context context ){
LocationManager locationManager = (LocationManager) context
. GetSystemService (Context. LOCATION_SERVICE ));
List AccessibleProviders = locationManager. getProviders (true );
Return accessibleProviders! = Null & accessibleProviders. size ()> 0;
}

/**
* Whether wifi is enabled
*/
Public static boolean isWifiEnabled (Context context ){
ConnectivityManager mgrConn = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE );
TelephonyManager mgrTel = (TelephonyManager) context
. GetSystemService (Context. TELEPHONY_SERVICE );
Return (mgrConn. getActiveNetworkInfo ()! = Null & mgrConn
. GetActiveNetworkInfo (). getState () = NetworkInfo. State. CONNECTED) | mgrTel
. GetNetworkType () = TelephonyManager. NETWORK_TYPE_UMTS );
}

/**
* Determine whether the current network is a Wi-Fi network
* If (activeNetInfo. getType () = ConnectivityManager. TYPE_MOBILE) {// judge the 3G network
*
* @ Param context
* @ Return boolean
*/
Public static boolean isWifi (Context context ){
ConnectivityManager connectivityManager = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager. getActiveNetworkInfo ();
If (activeNetInfo! = Null
& ActiveNetInfo. getType () = ConnectivityManager. TYPE_WIFI ){
Return true;
}
Return false;
}

/**
* Determine whether the current network is a 3G network
*
* @ Param context
* @ Return boolean
*/
Public static boolean is3G (Context context ){
ConnectivityManager connectivityManager = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager. getActiveNetworkInfo ();
If (activeNetInfo! = Null
& ActiveNetInfo. getType () = ConnectivityManager. TYPE_MOBILE ){
Return true;
}
Return false;
}
}
There are two other ways to determine whether the Network is available:

Public static boolean isNetworkAvailable_00 (Context context ){
ConnectivityManager cm = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE ));
If (cm! = Null ){
NetworkInfo info = cm. getActiveNetworkInfo ();
If (info! = Null & info. isConnectedOrConnecting ()){
Return true;
}
}
Return false;
}

Public static boolean isNetworkAvailable_01 (Context context ){
ConnectivityManager cm = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE );
NetworkInfo network = cm. getActiveNetworkInfo ();
If (network! = Null ){
Return network. isAvailable ();
}
Return false;
}
More rigorous writing:
Public static boolean checkNet (Context context ){

Try {
ConnectivityManager connectivity = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE );
If (connectivity! = Null ){

NetworkInfo info = connectivity. getActiveNetworkInfo ();
If (info! = Null & info. isConnected ()){

If (info. getState () = NetworkInfo. State. CONNECTED ){
Return true;
}
}
}
} Catch (Exception e ){
Return false;
}
Return false;
}

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.