Check Internet connection in Android
Just use of this method, checking that android device have internet connection or not.
This method return true, if Internet connection is available in android device, else return false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class ConnectionStatus { /** * <a>This method return true if the Internet connection are available else false</a> * @param context * @return return true if the Internet is available else false. */ public static boolean isOnline(Context context) { boolean status = false; try { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED) { status = true; } else { netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED) { status = true; } } } catch (Exception e) { e.printStackTrace(); return false; } return status; } }
You have to add permission in manifest file.
|
1 | <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
No comments:
Post a Comment