× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS
Rajen Raiyarela
Aug 03, 2016
<p>I have noticed, for checking whether device has network or not, most of the developer check whether device network status and see if Mobile Data or Wifi is ON and its status is connected or is_connecting as below:</p> <pre><code class="language-java">ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager.getActiveNetworkInfo(); if (networkInfo != null &amp;&amp; networkInfo.isConnected()) { if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE || networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { return true; } else if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { return true; }</code></pre> <p>But one thing i have noticed, especially for the client using the application where there is no good Internet connectivity, device will show connected to Wifi but your Wifi router may not be connected to internet. So it is always good to add an extra layer of checking but checking any specific web url like http:.//google.com. Suggested URL is your application's web service url.</p> <p>For that i have written this class which i include in my all applicatoin</p> <pre><code class="language-java"> public class ApplicationConnectivityCheckerUtil{ public interface ReachabilityTestCallback { void onReachabilityTestPassed(); void onReachabilityTestFailed(); } public interface ConnectionNotifier { void onConnectionChange(boolean isConnected); } public static ConnectionNotifier connectionNotifier = null; private static ReachabilityTest mReachabilityTest = null; public static void checkConnectivity(Context context) { String ping_url = "http://google.com"; if (mReachabilityTest == null || (mReachabilityTest != null &amp;&amp; mReachabilityTest.getStatus() != AsyncTask.Status.RUNNING)) { mReachabilityTest = su.new ReachabilityTest(context, ping_url, new ReachabilityTestCallback() { @Override public void onReachabilityTestPassed() { SupportUtil.IS_NETWORK_AVAILABLE = true; if (connectionNotifier != null) connectionNotifier.onConnectionChange(true); } @Override public void onReachabilityTestFailed() { SupportUtil.IS_NETWORK_AVAILABLE = false; if (connectionNotifier != null) connectionNotifier.onConnectionChange(false); } }); mReachabilityTest.execute(); } } private class ReachabilityTest extends AsyncTask&lt;Void, Void, Boolean&gt; { private Context mContext; private String url; private ReachabilityTestCallback mCallback; ReachabilityTest(Context context, String url, ReachabilityTestCallback callback) { mContext = context.getApplicationContext(); this.url = url; mCallback = callback; } @Override protected Boolean doInBackground(Void... args) { if (isCancelled()) return false; if (isConnected(mContext)) { if (isCancelled()) return false; InetAddress address = isResolvable(url); if (address != null) { return true; } } return false; } @Override protected void onPostExecute(Boolean result) { super.onPostExecute(result); if (mCallback != null) { if (result) { mCallback.onReachabilityTestPassed(); } else { mCallback.onReachabilityTestFailed(); } } } private boolean isConnected(Context context) { ConnectivityManager manager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager.getActiveNetworkInfo(); if (networkInfo != null &amp;&amp; networkInfo.isConnected()) { if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE || networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { return true; } else if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { return true; } } return false; } private InetAddress isResolvable(String url) { try { String hostname = new URL(url).getHost(); return InetAddress.getByName(hostname); } catch (UnknownHostException e) { e.printStackTrace(); return null; } catch (MalformedURLException e) { e.printStackTrace(); return null; } } } }</code></pre> <p>To use it, call ApplicationConnectivityCheckerUtil.checkConnectivity(context); from your BroadcastReceiver which handles ConnectivityManager.CONNECTIVITY_ACTION &amp; NETWORK_STATE_CHANGED_ACTION events.</p> <p>Also from all the new activity or fragment you load, make sure to set the value of ApplicationConnectivityCheckerUtil.connectionNotifier to the local instance of ConnectionNotifier <span style="color:rgb(95, 99, 102)">declared in the class</span>, to get notified for any change in connectivity and handle the application logic likewise.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus