在 Android 上检测网络连接? [英] Detecting network connectivity on Android?

查看:34
本文介绍了在 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.

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

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 似乎是目前最可靠的,但我想知道人们是否也成功使用了其他侦听器.

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.

推荐答案

由于 StackOverflow 不允许我关闭问题,否则我将提供我的 PhoneStateListeners 无法正常工作的原因:

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

我发现我的问题只是我没有以按位方式注册我的听众,而是连续(即
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................);)

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

我将关闭这个问题,也许稍后会打开一个不同的问题,询问有关在 Android 上保持与服务器的始终活动"连接的设计建议.谢谢.

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.

这篇关于在 Android 上检测网络连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆