requestRouteToHost问题在4.0.3和4.0.4下,但不是其他问题

时间:2013-01-17 09:11:20

标签: android device android-version

对于个人项目,我必须检查设备LAN ip for MMS是否可以访问。 我已经使用提供的APN找到了IP。现在我需要使用requestRouteToHost方法来检查找到的代理是否有效。

代码完全适用于运行android 2.3到4.0以及4.1到4.1以上的设备。 但它在4.0.3和4.0.4下引发了一个错误。我已在多台设备上测试过它,这不是设备问题,而是版本1。

if (!connMgr.requestRouteToHost(2, inetAddr)) {
     Log.e(TAG, "unable to request route to host");
     throw new IOException("Cannot establish route to proxy " + inetAddr);
} else {
     Log.e(TAG, "route to host requested");
}

第一个条件总是正确的,这是一个真正的问题。

上面的代码包含在我做的AsynTask中:

private class AsyncRoute extends AsyncTask<String, Void, Void>
{
    @Override
    protected Void doInBackground(String... proxyAddr)
    {
        try
        {
            ensureRouteToHost(proxyAddr[0]);
            Log.e(TAG, "OK ACK");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        mInetAck = true;
        return null;
    }
}

找到的InetAddress为-938825536,相当于192.168.010.200

要启动此AsyncTask,我使用以下代码:

new AsyncRoute().execute(MMSProxy);
while (!mInetAck);

基本上,它会在做几个请求之前等待确认。

那么,有人知道是否有一个技巧可以绕过这个问题?我的意思是有没有办法像connMgr.requestRouteToHost(2, inetAddr)那样可以在每个设备上运行?

感谢。

Ps:在Desire S 2.3,Desire S 4.0.3,Xperia lt30p 4.0.4,Nexus S 4.0.3等上进行测试。

编辑:在抛出异常的设备上,路由IP上的ping命令有效:

> adb shell ping 192.168.10.200
PING 192.168.10.200 (192.168.10.200) 56(84) bytes of data.
64 bytes from 192.168.10.200: icmp_seq=1 ttl=251 time=4023 ms
64 bytes from 192.168.10.200: icmp_seq=5 ttl=251 time=81.3 ms

但路线请求失败:s

1 个答案:

答案 0 :(得分:0)

问题解决了。 启用wifi后,无法找到IP路由。最简单的方法是禁用wifi,做你的东西,然后启用wifi。

以下是我使用的代码:

// Disable wifi if it's active
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled())
{
      mWasWifiActive = true;
      wifiManager.setWifiEnabled(false);
      Log.e(TAG, "Wifi was enabled, now Off.");
}

// Do stuff here

// Re-enable wifi if it was active before routing
if (mWasWifiActive)
{
       WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
       wifiManager.setWifiEnabled(true);
       Log.e(TAG, "Wifi is back online.");
}