Detecting “use only 2G networks” setting

前端 未结 2 1267
面向向阳花
面向向阳花 2021-02-09 05:36

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

2条回答
  •  鱼传尺愫
    2021-02-09 06:15

    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!

提交回复
热议问题