четверг, 22 сентября 2011 г.

Android snippet: Check if the device is connected to the internet

Just a quick function to make your live easier
How to check if the device that runs your application has an active internet connection
Here is the function:
public static boolean isOnline(Context context) {
     ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     NetworkInfo netInfo = cm.getActiveNetworkInfo();
     if (netInfo != null && netInfo.isAvailable() && netInfo.isConnected()) {
         return true;
     }
     return false;
}
Also You must add the INTERNET and ACCESS_NETWORK_STATE permissions to Your application
<uses-permission android:name="android.permission.INTERNET"  />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

Комментариев нет:

Отправить комментарий