Pages
How to check if the phone is connected to the Internet
11:21 PM
Posted by Mina Samy
ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
boolean internet=con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
these lines check for internet connection whether it is through WiFi or Mobile Internet.
you need to add the following permission to the AndroidManifest.xml file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Another method is what our freind Kevin mentioned in the comments, is to check if a certain website is reachable or not like this:
public static boolean isOnline() {
try {
InetAddress.getByName("google.ca").isReachable(3);
return true;
} catch (UnknownHostException e){
return false;
} catch (IOException e){
return false;
}
}
we check if a site is reachable within a certain timeout or not.
| Reactions: |
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
internet connection
. Follow any responses to this post through RSS. You can leave a response, or trackback from your own site.
Subscribe to:
Post Comments (Atom)









Post a Comment