The Android Development Network

Source: Internet
Author: User

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.

 

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.