检测“仅使用2G网络”环境

发布于 2024-11-17 15:05:04 字数 613 浏览 2 评论 0原文

有没有办法返回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 技术交流群。

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

发布评论

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

评论(2

从﹋此江山别 2024-11-24 15:05:04

对于一小部分设备(特别是 LG Optimus 2X Speed、LG-P990),答案似乎是:

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

其中“仅使用 2G 网络”设置指定为:

  • 0 表示该设置已禁用
  • 1 表示该设置已启用
  • -1 表示该设置未设置(某些设备?)

我是如何发现这一点的?我使用以下方法从 Settings.Secure 收集了所有键/值对:

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

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

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

不要使用索引列(上例中的5154),因为我注意到它在切换设置之间发生变化。

尽管这与 一些设置文档相关.安全我在网上发现,并不是所有手机都遵循这个值。

如果您的设备返回 -1,也许列出键值对将显示您需要的设置。如果遇到请评论!

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

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

  • 0 indicates the setting is disabled
  • 1 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:

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): cursor: 5154, preferred_network_mode, 1

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!

蓝天 2024-11-24 15:05:04

据我所知,没有记录的方法可以获取该设置的价值。但是有一个 Use2GOnlyCheckBoxPreference 类可以作为例子。它使用内部 电话PhoneFactory 类获取prefer_2g设置的当前值。

您可以通过反射使用 PhonePhoneFactory 类。但当然这是没有记录的,风险由您自行承担。以下是来自 Use2GOnlyCheckBoxPreference 的相关代码:

import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneFactory;

public class Use2GOnlyCheckBoxPreference extends CheckBoxPreference {

    private Phone mPhone;
    private MyHandler mHandler;

    public Use2GOnlyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mPhone = PhoneFactory.getDefaultPhone();
        mHandler = new MyHandler();
        mPhone.getPreferredNetworkType(
                mHandler.obtainMessage(MyHandler.MESSAGE_GET_PREFERRED_NETWORK_TYPE));
    }

    private class MyHandler extends Handler {

        private static final int MESSAGE_GET_PREFERRED_NETWORK_TYPE = 0;

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_GET_PREFERRED_NETWORK_TYPE:
                    handleGetPreferredNetworkTypeResponse(msg);
                    break;
            }
        }

        private void handleGetPreferredNetworkTypeResponse(Message msg) {
            AsyncResult ar = (AsyncResult) msg.obj;

            if (ar.exception == null) {
                int type = ((int[])ar.result)[0];
                Log.i(LOG_TAG, "get preferred network type="+type);
                setChecked(type == Phone.NT_MODE_GSM_ONLY);
            } else {
                // Weird state, disable the setting
                Log.i(LOG_TAG, "get preferred network type, exception="+ar.exception);
                setEnabled(false);
            }
        }
    }   
}

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 internal Phone and PhoneFactory classes to obtain the current value of prefer_2g setting.

You can use Phone and PhoneFactory classes via reflection. But of cause this is undocumented and is on your own risk. Here is relevant code from Use2GOnlyCheckBoxPreference:

import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneFactory;

public class Use2GOnlyCheckBoxPreference extends CheckBoxPreference {

    private Phone mPhone;
    private MyHandler mHandler;

    public Use2GOnlyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mPhone = PhoneFactory.getDefaultPhone();
        mHandler = new MyHandler();
        mPhone.getPreferredNetworkType(
                mHandler.obtainMessage(MyHandler.MESSAGE_GET_PREFERRED_NETWORK_TYPE));
    }

    private class MyHandler extends Handler {

        private static final int MESSAGE_GET_PREFERRED_NETWORK_TYPE = 0;

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_GET_PREFERRED_NETWORK_TYPE:
                    handleGetPreferredNetworkTypeResponse(msg);
                    break;
            }
        }

        private void handleGetPreferredNetworkTypeResponse(Message msg) {
            AsyncResult ar = (AsyncResult) msg.obj;

            if (ar.exception == null) {
                int type = ((int[])ar.result)[0];
                Log.i(LOG_TAG, "get preferred network type="+type);
                setChecked(type == Phone.NT_MODE_GSM_ONLY);
            } else {
                // Weird state, disable the setting
                Log.i(LOG_TAG, "get preferred network type, exception="+ar.exception);
                setEnabled(false);
            }
        }
    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文