Determine whether a network connection is available in Android

Source: Internet
Author: User

1. Determine whether the network connection is available

Public static Boolean isnetworkavailable (context) {connectivitymanager CM = (connectivitymanager) context. getsystemservice (context. connectivity_service); If (Cm = NULL) {} else {// you can use cm if it is only used to determine the network connection. getactivenetworkinfo (). isavailable (); networkinfo [] info = cm. getallnetworkinfo (); If (info! = NULL) {for (INT I = 0; I <info. length; I ++) {If (info [I]. getstate () = networkinfo. state. connected) {return true ;}}} return false ;}

 

The following code not only determines if the network is not enabled, but also opens the interface for enabling the network. The specific code is as follows:

Protected Boolean checknetwork () {// todo auto-generated method stubboolean flag = false; connectivitymanager cwjmanager = (connectivitymanager) getsystemservice (context. connectivity_service); If (cwjmanager. getactivenetworkinfo ()! = NULL) Flag = cwjmanager. getactivenetworkinfo (). isavailable (); If (! Flag) {builder B = new alertdialog. builder (this ). settitle ("no available network "). setmessage ("enable GPRS or WiFi network connection"); B. setpositivebutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int which) {// todo auto-generated method stubintent mintent = new intent ("/"); componentname comp = new componentname ("com. android. settings "," com. android. settings. wirelesssettings "); mintent. setcomponent (COMP); mintent. setaction ("android. intent. action. view "); startactivity (mintent );}}). setnegativebutton ("cancel", new dialoginterface. onclicklistener () {public void onclick (dialoginterface diich, int which) {// todo auto-generated method stub dialog. cancel ();}}). create (); B. show ();} return flag ;}

It can be used as follows:
If (! Checknetwork () return;
 

 

Ii. Determine if GPS is enabled

  public static boolean isGpsEnabled(Context context) {           LocationManager lm = ((LocationManager) context                   .getSystemService(Context.LOCATION_SERVICE));           List<String> accessibleProviders = lm.getProviders(true);           return accessibleProviders != null && accessibleProviders.size() > 0;       } 

Iii. Determine 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);       } 

4. Determine whether the network is a 3G network

 

    public static boolean is3rd(Context context) {           ConnectivityManager cm = (ConnectivityManager) context                   .getSystemService(Context.CONNECTIVITY_SERVICE);           NetworkInfo networkINfo = cm.getActiveNetworkInfo();           if (networkINfo != null                   && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {               return true;           }           return false;       }  

5. Determine whether it is WIFI or 3G network. The user's embodiment is here. WiFi can be downloaded or played online.

  public static boolean isWifi(Context context) {               ConnectivityManager cm = (ConnectivityManager) context                       .getSystemService(Context.CONNECTIVITY_SERVICE);               NetworkInfo networkINfo = cm.getActiveNetworkInfo();               if (networkINfo != null                       && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {                   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.