Android platform provides a Android.net.ConnectivityManager class to monitor the current network status including WiFi, GPRS, UMTS and so on. You can determine whether the current user network is WiFi or mobile network, you can also determine whether it is WAP access or net access, this in the weak network test can consider these knowledge to build tools.
This class is used in the following ways:
1. Monitor the current network status
2. Broadcast when network status sends changes
3. Switch to another network when one network connection fails
4. Provide some APIs to allow applications to query the current better and poorer network state
Here's the code to check the network connection:
Boolean checknetwork () {
Instantiate Connectivitymanager
Connectivitymanager manager = (Connectivitymanager) This
. Getsystemservice (Context.connectivity_service);
Get Current Network information
Networkinfo info = Manager.getactivenetworkinfo ();
Determine if the connection
if (info = = NULL | |!info.isconnected ()) {
return false;
}
return true;
}
Some reference methods for network detection in Android weak net test