title: Java Android internet Access: WIFI or 3G
body: <p>In my android application it has to access a remote server to send a request and to receive the response to continue the functionality.</p>
  
  <p>I need to clarify that before send the request, do I have to specially select that it should be WIFI or Mobile(3G) Internet in a situation where both available. I am not sure that ANDROID OS itself will select the best among them Or will give an exception in runtime. I can't test it as I am working with emulator. Would like to know the standard way.</p>
  
  <p>I can check whether it has connected with WIFI or 3G(Mobile) by the following code. Like to know before I send the request if I have to select that it should be WIFI or MOBILE(3G) internet. A proper guidance is highly appreciated. Thanks in advance...</p>
  
  <pre><code>   ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
     NetworkInfo[] netInfo = cm.getAllNetworkInfo();
  
     for (NetworkInfo ni : netInfo) 
     {
         if (ni.getTypeName().equalsIgnoreCase("WIFI") &amp;&amp; ni.isConnected())
         {
              Toast.makeText(getApplicationContext(), "Connected to Internet with WIFI", Toast.LENGTH_LONG).show();
         }
         if (ni.getTypeName().equalsIgnoreCase("MOBILE") &amp;&amp; ni.isConnected())
         {
              Toast.makeText(getApplicationContext(), "Connected to Internet with 3G", Toast.LENGTH_LONG).show();
         }
     }
  </code></pre>