简体   繁体   English

Android-如何在WiFi可用时保持3G连接

[英]Android - How to maintain 3g connection when wifi available

My app connects with a persistent connection to a server. 我的应用程序通过与服务器的持久连接进行连接。 If the device is currently using 3G it will connect over 3g. 如果设备当前正在使用3G,它将连接3g以上。
The problem is that if it connected using 3G and moved into wifi the connection drops. 问题是,如果它使用3G连接并移入wifi,则连接断开。 How do I prevent it from disconnecting? 如何防止其断开连接?

Sounds like the server is unable to accommodate clients seamlessly switching to a different IP address, or it may be using something like keep-alive packets to maintain connection state, and when the 3G connection drops, it disconnects your session. 听起来好像服务器无法容纳客户端无缝切换到另一个IP地址,或者服务器正在使用诸如保持活动数据包之类的东西来维持连接状态,并且当3G连接断开时,它将断开您的会话。

This may not be something you have control over. 这可能不是您可以控制的。 From the Android point of view - the device will prefer WiFi depending on user preferences and you will likely not have any direct control over that either. 从Android的角度来看-该设备会根据用户的喜好选择WiFi,而您也可能对此没有任何直接控制权。

In a nutshell - if you can't modify or reconfigure the communications protocol to allow client IP changes on the fly, then there's nothing you can do with Android to mitigate the problem. 简而言之-如果您无法修改或重新配置通信协议以允许客户端IP即时更改,那么您无法使用Android来缓解此问题。

All mobile devices, and to a lesser extent, desktops/laptops will at some point change their public facing IP address, so it sounds like a bug or oversight in the server/protocol design to me. 所有移动设备,以及程度较小的台式机/笔记本电脑,都将在某个时候更改其面向公众的IP地址,因此对我来说,这听起来像是服务器或协议设计中的错误或疏忽。


EDIT: 编辑:

In response to your comment, and in the interests of UX, you should be very careful about finding a way to force your app (possibly even the entire device) to remain on 3G when the user has requested that it use WiFi. 为了回应您的评论,并出于UX的利益,您应该非常谨慎地寻找一种方法,以在用户请求其使用WiFi时强制将您的应用程序(可能甚至整个设备)保留在3G上。

Most people have capped data plans with their device and wouldn't be very pleased if they think they are using WiFi (which is most likely free, or at least no additional cost) when in fact you've forced their device to continue using potentially very expensive 3G data instead. 多数人已将数据计划限制在其设备上,如果他们认为自己正在使用WiFi(这很可能是免费的,或者至少没有额外费用),但实际上您强迫他们的设备继续使用时,可能不会感到非常满意而是使用非常昂贵的3G数据。

This is especially important when any method would likely be actively circumventing the reasonable limits the Android environment presents you with, and therefore would probably not be flagged as a "Service that costs you money" when installed. 当任何方法可能会积极地绕过Android环境为您提供的合理限制,因此在安装时可能不会被标记为“需要您付费的服务”时,这一点尤其重要。


EDIT 2: 编辑2:

So, there may be a way for you to do it, but it relies on unsupported, private Android APIs which may change at any moment - usual disclaimer applies. 因此,也许有一种方法可以实现,但是它依赖于不受支持的私有Android API,这些API可能随时更改-通常适用免责声明。

Take a look here where they access the ConnectivityManager object to allow you to enable mobile data. 在这里查看他们访问ConnectivityManager对象的位置,以允许您启用移动数据。

This method does require you to build against the Android source tree, and use a shared user ID with "system" so may or may not be suitable, but these APIs are private (as apps are not supposed to be able to do this without user action), but it may help you. 此方法确实需要您根据Android源代码树进行构建,并使用带有“ system”的共享用户ID,因此可能合适也可能不合适,但是这些API是私有的(因为应用程序不应在没有用户的情况下执行此操作动作),但可能会对您有所帮助。

This is how Android works. 这就是Android的工作方式。 You're app should not maintain a persistent connection, it should only open a connection when needed. 您的应用程序不应该维护持久连接,而应仅在需要时打开连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android设备上如何检查wifi或3g网络 - how to check wifi or 3g network is available on android device 在Android中检测连接是否为wifi,3G或EDGE? - Detect if connection is wifi, 3G or EDGE in android? 在Android中检查3G和W​​ifi的互联网连接 - Check internet connection with 3G and Wifi in Android 3g可用但无法访问时,Android检查互联网连接 - Android check internet connection when 3g is available but not accessible 如何在Android中开启3G,Wifi或GPS? - How to turn on 3G, Wifi or GPS in Android? Android:检查 3G 或 Wifi 网络是否在 Android 设备上打开或可用 - Android : Check 3G or Wifi network is ON or Available or not on android Device Android:如何以编程方式知道当前的连接类型(例如Wifi,3G,2G…)? - Android: How can we know the current connection type (ex. Wifi, 3G, 2G…) programmatically? 如何在Android应用中使用任何Internet连接(WiFi,3g,4g等)? - How to use any Internet connection in Android app (wifi, 3g, 4g, etc)? 当3G网络可用时,Android GCM无法与wifi网络配合使用 - Android GCM doesn't work with wifi networks when 3G network is available android应用程序在使用3g连接连接到webservice时挂起,并且可以正常使用WIFI - android application hangs when it connects to webservice using 3g connection and works fine with WIFI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM