在我的代码中,我使用requestRouteToHost()方法:
这个路由是否意味着将wifi改为3g,或者反之亦然??
我的代码不起作用…

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;
 }

谢谢

最佳答案

方法requestRouteToHost()不会将wifi更改为3g,反之亦然!
Official Documentation

public boolean requestRouteToHost (int networkType, int hostAddress)

确保存在网络路由,以便通过指定的网络接口将流量传递到指定主机。添加已存在路由的尝试被忽略,但被视为成功。
参数
networkType要路由到指定主机的流量的网络类型
hostAddress需要路由的主机的IP地址
退换商品
true on successfalse on failure

10-07 15:13