检测 Android 上的网络连接?

发布于 2024-10-19 02:35:42 字数 656 浏览 2 评论 0原文

操作系统:Android

假定:用户已声明他们希望保持与应用程序服务器的连接以接收更新等。

目标:确保即使应用程序处于后台状态,用户也能连接到应用程序的服务器。

问题:一个问题是偶尔会与网络断开连接。如果用户失去数据网络连接(失去 2G、3G、WiFi),然后又重新获得连接,我们的应用程序将失去连接。我目前正在尝试使用 PhoneStateListener 的目的是检测各种网络变化,从而在适当的时候重新启动与服务器的连接。在我的 Nexus One (2.1) 上,我发现 onSignalStrengthsChangedonDataConnectionStateChanged 不会被调用,除非最初注册侦听器,但之后不会调用。侦听器在服务内部注册,因此只要服务处于活动状态,它们就会持续侦听(出于本问题的目的,我们可以假设“永远”)。还有其他人在侦听数据连接状态时遇到过任何问题吗?

onServiceStateChanged 似乎是迄今为止最可靠的,但我想知道人们是否也与其他侦听器取得了成功。

OS: Android

Given: User has stated that they wish to remain connected to app's server to receive updates, etc.

Goal: To ensure that users are connected to app's server even when app is in background state.

Question: One problem has been occasional disconnects from the network. If a user loses data network connectivity (loss of 2G, 3G, WiFi) and then later regains connectivity, our app is left without a connection. I am currently trying to make use of PhoneStateListener's in order to detect various network changes and thereby restart connectivity with the server when appropriate. On my Nexus One (2.1), I find that onSignalStrengthsChanged and onDataConnectionStateChanged aren't called except when the listeners are originally registered, but not afterwards. The listeners are registered inside of a Service so they are continuously listening as long as the Service is alive (which we can assume to be 'forever' for purposes of this question). Has anyone else had any issues with listening to the state of the Data Connection?

onServiceStateChanged seems to be the most reliable so far, but I wanted to know if people have had success with the other listeners as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

远昼 2024-10-26 02:35:43

我认为您会为此使用 ConnectivityManager。

回答有关网络连接状态的查询的类。当网络连接发生变化时,它还会通知应用程序。通过调用 Context.getSystemService(Context.CONNECTIVITY_SERVICE) 获取此类的实例。

http://developer.android.com/reference/android/net/ConnectivityManager.html

根据文档,它监视网络连接并向应用程序发送有关更改的广播意图。

I would think you would use the ConnectivityManager for this.

Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get an instance of this class by calling Context.getSystemService(Context.CONNECTIVITY_SERVICE).

http://developer.android.com/reference/android/net/ConnectivityManager.html

per the docs, it monitors network connectivity and sends broadcast intents on change to applications.

北渚 2024-10-26 02:35:42

我想您必须定期发送保活消息来检查连接是否仍然存在。如果没有,请重新建立它。连接断开的原因有很多种,而且您无法检查所有这些客户端。

可能会考虑使用谷歌的云服务来完成你正在做的事情,因为他们已经为此保持了开放的连接。这样您的用户的手机就不会因为保持另一个连接而产生开销(这可能非常昂贵)

I guess you'll have to send keepalive messages at regular intervals to check whether the connection is still there. If not, reestablish it. There is a smorgasbord of reasons why your connection might drop, and you won't be able to check all of those client side.

Might consider using google's cloud service for what you're doing though, since they already keep an connection open for that. That way your user's phones won't have the overhead of keeping yet another connection around (which can be quite expensive)

违心° 2024-10-26 02:35:42

由于 StackOverflow 不允许我以其他方式关闭问题,因此我将提供为什么我的 PhoneStateListeners 无法工作的答案:

我发现我的问题只是我没有以按位方式注册监听器,而是连续注册(即
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
telephonyManager.listen(listener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
.
.
.

而不是(正确的):
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE..............);)

我将关闭这个问题,也许稍后会提出一个不同的问题有关在 Android 上保持与服务器的“始终活动”连接的设计建议。谢谢。

Since StackOverflow doesn't allow me to close the question otherwise, I will provide the answer to why my PhoneStateListeners were not working:

I discovered that my problem was simply that I wasn't registering my listeners in a bitwise manner, but rather successively (i.e.
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
telephonyManager.listen(listener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
.
.
.

instead of (the correct):
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE..............);)

I will close this question and perhaps later open a different question asking for design suggestion on maintaining 'always alive' connections to a server on Android. Thanks.

美男兮 2024-10-26 02:35:42

据我所知,当应用程序处于后台时,无法调用侦听器。您应该尝试使用服务而不是活动。

并且记住以正确的方式检索 TelephonyManager

TelephonyManager mTelephonyMgr = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE); 

As far as i know listeners could not be called when app is in background. You should try use services not activity.

And also remember to retrieve TelephonyManager in proper way

TelephonyManager mTelephonyMgr = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文