Android determines if the network is connected, jumps to the settings page and determines what network is connected

Source: Internet
Author: User

If you want to develop a network application program, first of all to consider whether to access the network, in the Android phone to determine whether the Internet can be determined by the Connectivitymanager class IsAvailable () method, first obtain the network communication class instance  Connectivitymanager cwjmanager= (Connectivitymanager) Getsystemservice (Context.connectivity_service); , use Cwjmanager.getactivenetworkinfo (). isavailable (); To return is valid, if True indicates that the current Android phone is connected to the Internet, may be WiFi or GPRS, HSDPA, and so on, the specific can be through the Connectivitymanager class of Getactivenetworkinfo () method to determine the detailed access method, it should be noted that the call needs to join <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" ></ Uses-permission> this permission, the Android Development network reminds everyone on the real computer market and the browser program used this method to determine whether to continue, while some network time-out can also check the existence of network connectivity, To avoid wasting power on the phone.

Determine if the network is connected to a resource address: http://download.csdn.net/detail/u014649598/7966901

First, determine whether the network connection is available, can not be used to jump to the settings interface

 @Override protected void OnStart () {Alertdialog.builder builder=new Builder (this); Check the user's network situation Connectivitymanager cm= (Connectivitymanager) MainActivity.this.getSystemService (context.connectivity_        SERVICE);        Returns the available network information Networkinfo Info=cm.getactivenetworkinfo ();                if (info!=null&&info.isconnected ()) {Toast.maketext (mainactivity.this, "network available", 0). Show ();        }else{Toast.maketext (Mainactivity.this, "Network Unavailable", 0). Show ();        Builder.settitle ("Reminder");        Builder.setmessage ("The current network is not available, click OK to set up the network"); Builder.setpositivebutton ("OK", new Onclicklistener () {@Override public void OnClick (Dialoginterface dialog, in        T which) {Intent intent=new Intent ();        Intent.setclassname ("Com.android.settings", "com.android.settings.Settings");        StartActivity (Intent);        }});        Builder.create (). Show ();    } super.onstart (); }


Second, use Connectionchangereceiver to determine whether the network is available

public class Mainactivity extends Activity {private Connectionchangereceiver myreceiver;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Use the broadcast to Judge Myreceiver=new Connectionchangereceiver (); This.registerreceiver (Myreceiver, New Intentfilter (ConnectivityM Anager.      connectivity_action)); }public class Connectionchangereceiver extends Broadcastreceiver {@Override public void onreceive (Context contex T, Intent Intent) {Connectivitymanager Connectivitymanager = (Connectivitymanager) context.getsystemservice (context .    Connectivity_service);    Networkinfo mobworkinfo = Connectivitymanager. Getnetworkinfo (Connectivitymanager.type_mobile);    Networkinfo wifiworkinfo = Connectivitymanager. Getnetworkinfo (Connectivitymanager.type_wifi);    Boolean isnetconnected = mobworkinfo.isconnected ();    Boolean iswificonnected = wifiworkinfo.isconnected (); Ifisnetconnected | |    iswificonnected) {System.out.println ("network Open");    LOG.E ("Wankai", "Wankai");    Boolean isconnected = true;    }else{Boolean isconnected = false;    SYSTEM.OUT.PRINTLN ("Network Off");    LOG.E ("Wankai2", "Wangguan"); }    }    }}
  

Third, judge whether the GPS is open

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;       


Iv. determine if WiFi is turned on

public static Boolean iswifienabled (context context) {           Connectivitymanager mgrconn = (connectivitymanager) context< C1/>.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);       


V. To determine whether it 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;       }  


Six, the judge is WiFi or 3g network, the embodiment of the user here, WiFi can be suggested to download or online playback.

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


Android determines if the network is connected, jumps to the settings page and determines what network is connected

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.