Android programming method for determining whether a network connection is available _android

Source: Internet
Author: User

The example in this article describes how Android programming determines whether a network connection is available. Share to everyone for your reference, specific as follows:

In order to improve the user experience, our first step in developing the Android app requires networking to get the data:

1. Determine if the current phone is open network

2. Whether the Internet can be opened

Then to implement the networking logic, to avoid not networking to do unnecessary work!

Normally, that's how we judge.

public static Boolean isnetavailable (context context) { 
  Connectivitymanager Connectmanager = (Connectivitymanager) Context.getsystemservice (context.connectivity_service);  
  Return (Connectmanager.getactivenetworkinfo ()!= null); 
 

But this completes only the first step, to determine whether the network is open,

Note: Opening does not mean you can surf the internet,

The observation found that Networkinfo had a method:

Copy Code code as follows:
Networkinfo.isavailable ()

The official explanation is that

Indicates whether network connectivity is possible. A network is unavailable then a persistent or semi-persistent condition prevents the possibility of connecting to Work. Examples include
The device is out of the "coverage area" for "any" network of this type.
The device is in a network other than the home network (i.e., roaming), and data roaming the has been.
The device ' s radio is turned off, e.g., because airplane the mode is enabled.
Returns:
True if the network is available, false otherwise

He cites several networks that are connected but not accessible to the Internet,

So let's just change it like this:

public static Boolean isnetavailable (context context) {
  Connectivitymanager manager = (Connectivitymanager) Context.getsystemservice (context.connectivity_service);
  Networkinfo info = manager.getactivenetworkinfo ();
  return (info!= null && info.isavailable ());
}

I hope this article will help you with the Android program.

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.