title: How to handle network change between wifi and mobile data?
body: <p>I am building a VoIP app.During a VoIP call when user switches between WiFi to mobile data i have a problem handling the scenario.</p>
  
  <p>In my call screen activity I have registered for receiver which helps me get notified about the network change scenarios.</p>
  
  <p>This is the code which I am using for detecting change in networks in the onRecieve Method. <code>conn_name</code> is private class level variable holding previous connection name.</p>
  
  <pre><code>ConnectivityManager connectivity_mgr = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
  NetworkInfo net_info = connectivity_mgr.getActiveNetworkInfo();
  
  if (net_info != null &amp;&amp; net_info.isConnectedOrConnecting() &amp;&amp; !conn_name.equalsIgnoreCase("")) {
      new_con = net_info.getExtraInfo();
      if (new_con != null &amp;&amp; !new_con.equalsIgnoreCase(conn_name))
          network_changed = true;
      conn_name = (new_con == null) ? "" : new_con;
      connectionStatus ="connected";
  } else {
      if (net_info != null &amp;&amp; conn_name.equalsIgnoreCase("")) {
          conn_name = net_info.getExtraInfo();
          connectionStatus ="connected";
          network_changed = true;
      } else if(!new_con.equals(conn_name)) {
          conn_name = "";
          connectionStatus ="disconnected";
          network_changed = true;
      }
  }
  </code></pre>
  
  <p>So using above method I able detect network changes. But one peculiar thing happens when I am connected with WiFi. When my app starts initially it is connected with mobile data.when user enters into his known WiFi area,he gets connected to his known WiFi. Since WiFi is always chosen as default route,android switches to WiFi and I receive the network notification that WiFi has been turned on.</p>
  
  <p>So I update my apps IP address to WiFi IP address, so no issues here. But still mobile data is still connected at the same time but <strong>getActiveNetworkInfo()</strong> tells me that I'm connected with WiFi clearly even if I was early connected to mobile data.</p>
  
  <p>So the problem is when user switches off the WiFi button,and mobile data is still connected but I still receive the notification for WiFi turn off.
  It indicates me that network is disconnected even when my phone is still connected to mobile data.</p>
  
  <p>But after a second I receive a notification that mobile data is connected.
  But once I receive the network disconnected, I have closed my VoIP call.
  So when I receive a notification for WiFi switched off how can I make sure whether mobile data is still connected.</p>
  
  <p>I tried getActiveNetworkInfo() but it happens to be null when I receive notification for WiFi turned off.</p>
  
  <p>I have followed this links:</p>
  
  <p><a href="https://stackoverflow.com/questions/6591774/android-api-call-to-determine-user-setting-data-enabled/7014203#7014203">Android API call to determine user setting &quot;Data Enabled&quot;</a><br>
  <a href="https://stackoverflow.com/questions/12806709/how-to-tell-if-mobile-network-data-is-enabled-or-disabled-even-when-connected/32239785#32239785">How to tell if &#39;Mobile Network Data&#39; is enabled or disabled (even when connected by WiFi)?</a>  </p>
  
  <p>Using the above link I am able detect that mobile data button has been enabled when user has connected with mobiledata.it gives me true.
  But the problem happens when this particular case happens.</p>
  
  <p>Now when wifi is disabled I get notification but it shows that mobile data is disabled even when my mobile data is enabled. I am not able to handle this situation as I disconnect my calls when I receive my disconnected notification.</p>