Network: Android Network judgment

Source: Internet
Author: User

There is nothing to say!

Package mark. zhang; import Java. util. list; import android. content. context; import android. location. locationmanager; import android.net. connectivitymanager; import android.net. networkinfo; import android. telephony. telephonymanager; public class networkprober {/*** network availability ** @ Param activity * @ return */public static Boolean isnetworkavailable (context) {connectivitymanager connectivity = (connectiv Itymanager) 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;}/*** determines if GPS is enabled ** @ Param context * @ return */public static Boolean isgpsenabled (context) {locationmanager = (locationmanager) context. getsystemservice (context. location_service); List <string> accessibleproviders = locationmanager. getproviders (True); Return accessibleproviders! = NULL & accessibleproviders. size ()> 0;}/*** whether WiFi is enabled */public static Boolean iswifienabled (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);}/*** determines whether the current network is a Wi-Fi network * If (activenetinfo. getType () = connectivitymanager. type_mobile) {// determine the 3G network ** @ Param context * @ return Boolean */public static Boolean iswifi (context) {connectivitymanager = (connectivitymanager) Conte XT. 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) {connectivitymanager = (connectivitymanager) context. getsystemservice (context. connectivity_service); networkinfo activenetinfo = connectivitymanager. getactivenetworkinfo (); If (activene Tinfo! = 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;}

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.