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

前端 未结 2 1130
无人共我
无人共我 2021-01-06 19:40

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...

相关标签:
2条回答
  • 2021-01-06 19:56

    I think this is an extremely poorly documented method, and while an above comment saying "consider it a ping" might be a reasonable interpretation, I don't think it's correct. The fact that it takes an int as a host address suggests it is a much lower-level method than that, and the comment in the JavaDoc This method requires the caller to hold the permission CHANGE_NETWORK_STATE is another clue, suggesting that this makes a change in the internal routing table of the device. This link provides a better explanation:

    requestRouteToHost() doesn't establish connectivity on any network, it only ensures that any traffic for the specified host will be routed via the specified network type (wifi or mobile). Connectivity must already exist on the specified network.

    This explanation makes MUCH more sense, considering the permission required. It also appears that it will not work with WiFi. So, it appears what this method is useful for is the following: You wish to ensure that the connection made to a particular host will be made via a SPECIFIC interface and that interface is not WiFi. This might make sense for a long-term, low traffic, battery efficient connection, such as when you wish to keep a socket open to a server and wait for the server to send the occasional message. The mobile data interface would make more sense than WiFi, since you wouldn't need to keep the WiFi radio active the whole time, and the mobile network radio is always on anyway. Incidentally, this is EXACTLY how an iPhone's server "push" mechanism works: It keeps a socket to an Apple server constantly connected over the mobile data interface, waiting for the server to say something.

    So, in opposition to the (currently chosen) correct answer, I suggest that the answer to the asker's question: Does this routing means changing the WIFI to 3G or vice versa?? is actually, "Yes, sort of!" If the method returns true, the caller is assured that connections to that IP address will happen over the indicated interface.

    And to Google: Boo on you for not documenting some of your APIs better!

    0 讨论(0)
  • 2021-01-06 20:13

    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

    0 讨论(0)
提交回复
热议问题