Tools related to android Networks

Source: Internet
Author: User

Tools related to android Networks

Tools related to android Networks

Stick it to the past and use it.

Package com. activity;

Import java. util. List;

Import android. content. Context;
Import android. location. LocationManager;
Import android.net. ConnectivityManager;
Import android.net. NetworkInfo;
Import android. telephony. TelephonyManager;

/**
* Network-related tools
*/
Public class NetUtils {

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

}

 

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.