/** * Declare Connection Manager Object * / private Connectivitymanager Connectivitymanager; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); 1. Get the Connection Manager Object this.connectivitymanager= (Connectivitymanager) This.getsystemservice through system services ( Context.connectivity_service); 2. Obtain the Network information object through the Connection Manager Object networkinfo networkinfo=this.connectivitymanager.getactivenetworkinfo (); if (networkinfo==null) { Toast.maketext (this, "current phone is not networked!", Toast.length_short). Show (); else{ String typename=networkinfo.gettypename (); System.out.println ("typename=" +typename); Toast.maketext (This, "Typename=" +typename, Toast.length_short). Show (); } }
2.
/** * Declare Connection Manager Object */private Connectivitymanager Connectivitymanager; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); 1. Get the Connection Manager Object This.connectivitymanager= (Connectivitymanager) This.getsystemservice through system services (context.connectivity_ SERVICE); 2. Obtain the Network information object through the Connection Manager Object Networkinfo networkinfo=this.connectivitymanager.getactivenetworkinfo (); if (networkinfo.isavailable ()) {Toast.maketext (this, "There is a network!", Toast.length_short). Show (); Get the network type int type= networkinfo.gettype (); Switch (type) {//mobile network case CONNECTIVITYMANAGER.TYPE_MOBILE:SYSTEM.OUT.PR INTLN ("mobile network"); Break WiFi Network case CONNECTIVITYMANAGER.TYPE_WIFI:SYSTEM.OUT.PRINTLN ("WiFi network"); Break }}eLSE {Toast.maketext (this, "No Network", Toast.length_short). Show (); } }
Android Network Status operation