Android Development Get Network link status _android

Source: Internet
Author: User

Web development is a very important part of Android programming, and today we share with you some of the experience of Android Web development.

In this paper, the method of acquiring network link state by Android is illustrated by the example form. The specific contents are as follows:

As far as the current Android phone is concerned, there may be 5 network states:

----No network (this state may be due to phone downtime, network is not open, bad signal, etc.)

----Use WiFi access

----Cmwap (China Mobile agent)

----Cmnet Internet

----2G/3G/4G Internet

Many times we need to determine whether a user is opening a network setting, usually by Connectivitymanager classes to determine whether a network connection exists.

Get Network Status:

So how do you use this class? How do you interact with the user? The specific examples are as follows:

public class Mainactivity extends activity {
  @Override
  protected 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 ()//give user prompt for network status
  }
  String Add (Boolean bl) {
    String s = "not available";
    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;
  }  
}

Certainly don't forget to get network permissions in the configuration file, the code is as follows:

<!--Get network permissions-->
  <uses-permission 
    android:name= "Android.permission.ACCESS_NETWORK_STATE"
    / >

I hope the example described in this article will help you with your Android programming.

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.