Search code examples
androidrouteswifi3g

Can you explain the Functionality of requestRouteToHost() in android?


In my code I am using requestRouteToHost() method:

Does this routing means changing the WIFI to 3G or vice versa??

My code is not working...

public static boolean isHostAvailable(Context context, String urlString) throws UnknownHostException, MalformedURLException { 
     boolean ret = false; 
     int networkType = ConnectivityManager.TYPE_WIFI; 
     ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     if(cm != null){ 
             NetworkInfo nf = cm.getActiveNetworkInfo(); 
             if(nf != null){ 
                     networkType = nf.getType(); 
             } 
             URL url = new URL(urlString); 
             InetAddress  iAddress = InetAddress.getByName(url.getHost()); 
             ret = cm.requestRouteToHost(networkType, ipToInt(iAddress.getHostAddress())); 
     } 
     return ret; 
}

public static int ipToInt(String addr) {
     String[] addrArray = addr.split("\\.");

     int num = 0;
     for (int i=0;i<addrArray.length;i++) {
         int power = 3-i;

         num += ((Integer.parseInt(addrArray[i])%256 * Math.pow(256,power)));
     }
     return num;
 }

Thanks


Solution

  • Method requestRouteToHost() does not change wifi to 3G or vice versa!

    Official Documentation :

    public boolean requestRouteToHost (int networkType, int hostAddress) 
    

    Ensure that a network route exists to deliver traffic to the specified host via the specified network interface. An attempt to add a route that already exists is ignored, but treated as successful.

    • Parameters

      networkType the type of the network over which traffic to the specified host is to be routed

      hostAddress the IP address of the host to which the route is desired

    • Returns

      true on success, false on failure