Many of the novice Android developers feel very big on the network, and now I will share with you my learning experience.
In the current Android phone, there may be 5 network statuses:
----No network (this may be due to phone downtime, network is not turned on, bad signal, etc.)
----Use WiFi internet access
----Cmwap (China Mobile agent)
----Cmnet Internet access
----2G/3G/4G Internet access
Many times we need to determine whether the user is turning on the network settings, usually through the Connectivitymanager class to determine whether the network connection exists.
Get Network Status:
So how do you use this class? How do I interact with the user? Share my experience with you now:
public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Connectivitymanager NW = (Connectivitymanager) this.getsystemservice (Context.connectivity_service); Networkinfo NetInfo = Nw.getactivenetworkinfo (); Toast.maketext (mainactivity.this, "Current Network" +add (Netinfo.isavailable ()) + "," + "Network" +app (netinfo.isconnected ()) + "," + " Network Connection "+ADP (netinfo.isconnected ()), Toast.length_long). Show ();//Prompts the user for the network state}string Add (Boolean bl) {String s =" unavailable "; if ( Bl==true) {s= "available";} return s;} String app (Boolean bl) {String s = "Not Connected"; if (bl==true) {s= "Connected";} return s;} string ADP (Boolean bl) {String s = "does not exist! "; if (bl==true) {s=" exists! ";} return s;}}
Sure, don't forget to get network permissions in the configuration file:
<!--gain Network privileges-- <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" / >
Because beginners, if there is no place, but also hope that the great God guidance, welcome to give their own views, and good settings.