List of utility methods to do Network State Check
int | getNetworkState(Context context) get Network State if (null != context) { ConnectivityManager connManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); State state; NetworkInfo networkInfo; if (null != connManager) { networkInfo = connManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); ... |
boolean | isLocationEnabledNetwork(Context c) is Location Enabled Network LocationManager lm = (LocationManager) c
.getSystemService(Context.LOCATION_SERVICE);
return lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
|
boolean | isOpenNetwork(Context context) is Open Network ConnectivityManager connManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connManager.getActiveNetworkInfo() != null) { return connManager.getActiveNetworkInfo().isAvailable(); return false; |
Bitmap | loadImageFromNetwork(String photoUrl) load Image From Network Bitmap bm = null; try { InputStream in = new java.net.URL(photoUrl).openStream(); bm = BitmapFactory.decodeStream(in); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); return bm; |
boolean | IsInternetReachable() Is Internet Reachable try { InetAddress.getByName("www.google.com").isReachable(3); return true; } catch (UnknownHostException e) { return false; } catch (IOException e) { return false; |
void | setMobileDataEnabled(Activity a, Context context, boolean enabled) set Mobile Data Enabled final ConnectivityManager conman = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); Class conmanClass = null; try { conmanClass = Class.forName(conman.getClass().getName()); } catch (ClassNotFoundException e) { Field iConnectivityManagerField = null; ... |
boolean | isNetworkAvailable(Activity mainActivity) is Network Available ConnectivityManager connectivityManager = (ConnectivityManager) mainActivity
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
|
boolean | isDeviceOnline(Activity activity) Iterates all network infos and returns true if one of them is available for sending data. ConnectivityManager cm = (ConnectivityManager) activity .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] netInfo = cm.getAllNetworkInfo(); if (netInfo != null) { for (int i = 0; i < netInfo.length; i++) { if (netInfo[i].isConnected()) return true; return false; |
boolean | isConnected(final Activity act) is Connected final ConnectivityManager connMgr = (ConnectivityManager) act .getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { return true; return false; |
boolean | isOnline(Activity activity) is Online ConnectivityManager cm = (ConnectivityManager) activity .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { return true; return false; |