检测“仅使用2G网络”环境
有没有办法返回Android移动网络设置“仅使用2G网络”的值?
正在开发的应用程序可以测量某个位置的互联网速度,但为了使其具有相关性,它必须知道用户是否故意将移动互联网限制为 2G。
我查看了 ConnectivityManager,但它仅提供有关后台数据设置或所有网络的信息。迭代它们表明,尽管启用了该设置,HSPA 和 UMTS 仍为 isAvailable()
返回 true
:
for (NetworkInfo netInfo : cm.getAllNetworkInfo()) {
Log.i(TAG, netInfo.getSubtypeName() + ": " + netInfo.isAvailable());
}
我在所有这些中发现的唯一提示是 当启用该设置时,netInfo.getReason()
在 HSPA 和 UMTS 上返回“connectionDisabled”。问题是,当禁用该设置时,这些网络类型根本不一定出现在列表中。在我看来,专门在 HSPA 和 UMTS 上使用字符串比较来表示“connectionDisabled”似乎并不合适。
解决这个问题的正确方法是什么?
Is there a way of returning the value of Android's mobile network setting for "use only 2G networks"?
The app being developed measures the internet speed at a certain location, but in order for this to be relevant, it must know if the user is deliberately restricting mobile internet to 2G.
I've taken a look at ConnectivityManager, but it only provides information about the background data setting or all networks. Iterating through them reveals that despite the setting being enabled, HSPA and UMTS return true
for isAvailable()
:
for (NetworkInfo netInfo : cm.getAllNetworkInfo()) {
Log.i(TAG, netInfo.getSubtypeName() + ": " + netInfo.isAvailable());
}
The only hint I've found amidst all this is that netInfo.getReason()
returns "connectionDisabled" on HSPA and UMTS when the setting is enabled. The trouble is, when the setting is disabled, those network types don't necessarily appear in the list at all. It doesn't seem right to me to use a string comparison specifically on HSPA and UMTS for "connectionDisabled".
What's the right way of tackling this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于一小部分设备(特别是 LG Optimus 2X Speed、LG-P990),答案似乎是:
其中“仅使用 2G 网络”设置指定为:
0
表示该设置已禁用1
表示该设置已启用-1
表示该设置未设置(某些设备?)我是如何发现这一点的?我使用以下方法从
Settings.Secure
收集了所有键/值对:我比较了启用和禁用设置之间的结果,果然我得到了:
不要使用索引列(上例中的5154),因为我注意到它在切换设置之间发生变化。
尽管这与 一些设置文档相关.安全我在网上发现,并不是所有手机都遵循这个值。
如果您的设备返回
-1
,也许列出键值对将显示您需要的设置。如果遇到请评论!For a small subset of devices (specifically for the LG Optimus 2X Speed, LG-P990), an answer seems to be:
Where the "use only 2G networks" setting is specified as:
0
indicates the setting is disabled1
indicates the setting is enabled-1
indicates the setting is not set (some devices?)How I discovered this? I gathered all the key/value pairs from
Settings.Secure
using the following:I compared results between enabling and disabling the setting, and sure enough I got:
Do NOT use the index column (5154 in the example above), as I've noticed it changes between toggling the setting.
Although this correlates with some documentation for Settings.Secure I found online, this value isn't respected by all phones.
If your device returns
-1
, perhaps listing the key value pairs will reveal which setting you need. Please comment if you encounter it!据我所知,没有记录的方法可以获取该设置的价值。但是有一个
Use2GOnlyCheckBoxPreference
类可以作为例子。它使用内部电话
和PhoneFactory
类获取prefer_2g
设置的当前值。您可以通过反射使用
Phone
和PhoneFactory
类。但当然这是没有记录的,风险由您自行承担。以下是来自Use2GOnlyCheckBoxPreference
的相关代码:As far as I can tell, there is no documented way of getting value for that setting. But there is a
Use2GOnlyCheckBoxPreference
class that can be used as an example. It uses internalPhone
andPhoneFactory
classes to obtain the current value ofprefer_2g
setting.You can use
Phone
andPhoneFactory
classes via reflection. But of cause this is undocumented and is on your own risk. Here is relevant code fromUse2GOnlyCheckBoxPreference
: