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.