检测"只使用2G网络"环境环境、网络、QUOT

2023-09-05 03:27:06 作者:じ追憶╮那似水流年づ

有返回Android的移动网络设置值仅使用2G网络?

Is there a way of returning the value of Android's mobile network setting for "use only 2G networks"?

该应用程序正在制定措施互联网的速度在某个位置,但为了这是相关的,它必须知道用户是否故意制约移动互联网到2G。

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.

我已经采取了看看ConnectivityManager,但它只提供有关背景资料设置或所有网络信息。通过他们迭代表明,尽管已启用该设置,HSPA和UMTS返回 isAvailable()

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());
}

我发现在这种情形下,唯一的暗示是, netInfo.getReason()当启用该设置返回connectionDisabled对HSPA和UMTS。麻烦的是,当设置被禁用,这些网络类型并不一定出现在列表中的。它似乎没有权利我使用字符串比较特别的HSPA和UMTS的connectionDisabled。

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?

推荐答案

有关设备(具体为LG的Optimus 2X速度,LG-P990)的一小部分,答案似乎是:

For a small subset of devices (specifically for the LG Optimus 2X Speed, LG-P990), an answer seems to be:

int enabled = Settings.Secure.getInt(getContentResolver(),
        "preferred_network_mode", -1);
Log.d("MYAPP", "2G only enabled: " + enabled);

当仅使用2G网络设置被指定为:

Where the "use only 2G networks" setting is specified as:

0 表示设置为禁用 1 表示启用该设置 1 表示没有设置(有些设备?) 0 indicates the setting is disabled 1 indicates the setting is enabled -1 indicates the setting is not set (some devices?)

我怎么发现的?我收集了所有键/值对从 Settings.Secure 使用以下内容:

How I discovered this? I gathered all the key/value pairs from Settings.Secure using the following:

ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(Settings.Secure.CONTENT_URI, null, null, null, null);
if (cursor.moveToFirst()) {
    while (!cursor.isAfterLast()) {
        Log.d("MYAPP", "cursor: "
                + cursor.getString(0) + ", "
                + cursor.getString(1) + ", "
                + cursor.getString(2));
        cursor.moveToNext();
    }
}

我比较启用和禁用设置之间的结果,果然我:

I compared results between enabling and disabling the setting, and sure enough I got:

07-08 00:15:20.991:DEBUG / MYAPP(13813):光标:5154,preferred_network_mode,1

07-08 00:15:20.991: DEBUG/MYAPP(13813): cursor: 5154, preferred_network_mode, 1

不要使用索引列(5154在上面的例子),因为我已经注意到了这一点切换设置之间变化。

Do NOT use the index column (5154 in the example above), as I've noticed it changes between toggling the setting.

尽管这与相关some对于Settings.Secure我在网上找到文档,这个值是不是所有手机都尊重。

Although this correlates with some documentation for Settings.Secure I found online, this value isn't respected by all phones.

如果您的设备返回 1 ,也许列出键值对,就会发现其中的设置需要。请评论,如果你遇到它!

If your device returns -1, perhaps listing the key value pairs will reveal which setting you need. Please comment if you encounter it!