安卓4.0 4G切换

发布于 2024-12-22 10:27:26 字数 334 浏览 2 评论 0原文

适用于 Verizon LTE 版本的 Samsung Galaxy Nexus。

我的任务是编写一个小型应用程序,该应用程序将有效禁用/启用 4G 功能。这可以通过 settings > 手动完成移动网络>>网络模式并选择LTE/CDMA(启用4g)或CDMA(仅限3g)。

我还没有尝试过任何东西,因为 Android 开发不是我的强项。我正在寻找指导...示例、代码示例等。我假设这应该几乎是一句台词,但根据我的经验,Android 开发没有什么像看起来那么简单。

任何帮助将不胜感激。

This is for the Verizon LTE version of the Samsung Galaxy Nexus.

I am tasked with writing a tiny app that will effectively disable/enable 4G capability. This can be done manually via settings > mobile network > network mode and choosing either LTE/CDMA (4g enabled) or CDMA (3g only).

I have not tried anything yet because Android development isn't my strong suit. I am looking for guidance... examples, code samples etc. I am assuming this should almost be a one-liner, but it has been my experience that with Android development nothing is as simple as it appears.

Any help will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

两仪 2024-12-29 10:27:26

Settings.Secure 类中有一个对 SDK 隐藏的首选项

    /**
     * The preferred network mode   7 = Global
     *                              6 = EvDo only
     *                              5 = CDMA w/o EvDo
     *                              4 = CDMA / EvDo auto
     *                              3 = GSM / WCDMA auto
     *                              2 = WCDMA only
     *                              1 = GSM only
     *                              0 = GSM / WCDMA preferred
     * @hide
     */
    public static final String PREFERRED_NETWORK_MODE =
            "preferred_network_mode";

:可以对此使用反射,或者只是将常量本地化到您的项目中。这样做的问题是您无法更改此设置的值(与所有安全设置一样),您只能读取它。上述值并不是唯一可能的值,实际上还有一些值位于 com.android.internal.telephony.RILConstants 中,这些值再次对 SDK 隐藏,需要反射才能访问。

TelephonyManager 中还有另一个隐藏方法,但同样它是只读的,没有其他方法设置这个常数。这将准确地告诉您您想知道的内容,无论设备设置为“LTE/CDMA”(LTE_ON_CDMA_TRUE) 还是“仅限 CDMA”(LTE_ON_CDMA_FALSE):

/**
 * Return if the current radio is LTE on CDMA. This
 * is a tri-state return value as for a period of time
 * the mode may be unknown.
 *
 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
 * or {@link Phone#LTE_ON_CDMA_TRUE}
 *
 * @hide
 */
public int getLteOnCdmaMode() {
    try {
        return getITelephony().getLteOnCdmaMode();
    } catch (RemoteException ex) {
        // Assume no ICC card if remote exception which shouldn't happen
        return Phone.LTE_ON_CDMA_UNKNOWN;
    } catch (NullPointerException ex) {
        // This could happen before phone restarts due to crashing
        return Phone.LTE_ON_CDMA_UNKNOWN;
    }
}

根据我的研究,如果没有 root 访问权限并使用某些东西,您就无法制作这样的应用程序类似于命令行中的 setprop,但即便如此,您也可能需要重新启动整个 Telephony 进程才能使此设置生效。

最后,如果您仍然感兴趣,请参阅 com.android.phone.Settings 以了解系统如何处理此切换。它相当复杂,正如我提到的,需要普通 Android 应用程序不会授予的权限。

There is a preference in the Settings.Secure class that is hidden from the SDK:

    /**
     * The preferred network mode   7 = Global
     *                              6 = EvDo only
     *                              5 = CDMA w/o EvDo
     *                              4 = CDMA / EvDo auto
     *                              3 = GSM / WCDMA auto
     *                              2 = WCDMA only
     *                              1 = GSM only
     *                              0 = GSM / WCDMA preferred
     * @hide
     */
    public static final String PREFERRED_NETWORK_MODE =
            "preferred_network_mode";

You could use Reflection on this or just localize the constant to your project. The problem with this is that you cannot change the value of this setting (as with all secure settings), you can only read it. The aforementioned values are not the only possible ones, there are actually a few more located in com.android.internal.telephony.RILConstants, which is again hidden from the SDK and would require Reflection to access.

There is another hidden method in TelephonyManager, but again it is read only there is no other method for setting this constant. This would tell you exactly what you want to know, whether the device is set to "LTE/ CDMA" (LTE_ON_CDMA_TRUE) or "CDMA only" (LTE_ON_CDMA_FALSE):

/**
 * Return if the current radio is LTE on CDMA. This
 * is a tri-state return value as for a period of time
 * the mode may be unknown.
 *
 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
 * or {@link Phone#LTE_ON_CDMA_TRUE}
 *
 * @hide
 */
public int getLteOnCdmaMode() {
    try {
        return getITelephony().getLteOnCdmaMode();
    } catch (RemoteException ex) {
        // Assume no ICC card if remote exception which shouldn't happen
        return Phone.LTE_ON_CDMA_UNKNOWN;
    } catch (NullPointerException ex) {
        // This could happen before phone restarts due to crashing
        return Phone.LTE_ON_CDMA_UNKNOWN;
    }
}

From my research you could not make such an application without root access and using something like setprop from the command line, but even then you may need to restart the entire Telephony process in order for this setting to take effect.

Finally, if you are still interested see com.android.phone.Settings to see how the system handles this toggle. It is rather elaborate, and as I mentioned would require permissions that a normal Android application would not be granted.

月野兔 2024-12-29 10:27:26

我也有兴趣更改设置 WCDMA-only、WCDMA/LTE...

我找到了使用 root 权限更改 Settings.secure.* 的方法,如下所示。

    new ExecuteAsRootBase() {
        @Override
        protected ArrayList<String> getCommandsToExecute() {
            ArrayList<String> cmds = new ArrayList<String>();
            cmds.add("su -c 'chmod 755 "+mySqlite+"'");
            cmds.add("echo \"UPDATE secure SET value='"+ value +"' WHERE name='"+ key +"'; \" | "+mySqlite+" /data/data/com.android.providers.settings/databases/settings.db");
            //TODO: SQL injection can be done!!!
            return cmds;
        }
    }.execute();

这里介绍了 ExecuteAsRootBase,而mySqlite是“/data/data/”+context.getPackageName()+“/files/sqlite3”,其中sqlite3提前放入。

不过,设置Settings.secure.PREFERRED_NETWORK_MODE后,我们似乎必须调用com.android.internal.telephony.Phone.setPreferredNetworkType()进行切换(仅限WCDMA<=>WCDMA/LTE)。
我的手机(甚至设置 Settings.secure.PREFERRED_NETWORK_MODE = 2)连接到 LTE 网络...

I'm also interested in changing the settings WCDMA-only, WCDMA/LTE, ...

I found the way to change Settings.secure.* with root privilege as is shown the below.

    new ExecuteAsRootBase() {
        @Override
        protected ArrayList<String> getCommandsToExecute() {
            ArrayList<String> cmds = new ArrayList<String>();
            cmds.add("su -c 'chmod 755 "+mySqlite+"'");
            cmds.add("echo \"UPDATE secure SET value='"+ value +"' WHERE name='"+ key +"'; \" | "+mySqlite+" /data/data/com.android.providers.settings/databases/settings.db");
            //TODO: SQL injection can be done!!!
            return cmds;
        }
    }.execute();

ExecuteAsRootBase is introduced here, and mySqlite is "/data/data/"+context.getPackageName()+"/files/sqlite3" where sqlite3 is put in advance.

However, it seems that we have to call com.android.internal.telephony.Phone.setPreferredNetworkType() for switching (WCDMA only<=>WCDMA/LTE) after setting Settings.secure.PREFERRED_NETWORK_MODE.
My phone (even set Settings.secure.PREFERRED_NETWORK_MODE = 2) attached to LTE network...

葬花如无物 2024-12-29 10:27:26

所有其他答案都是正确的,这需要访问 Settings.Secure。看看手机应用程序如何处理此设置 https://github.com/dzo/packages_apps_phone/blob/master/src/com/android/phone/Use2GOnlyCheckBoxPreference.java

或查看Toggle2G 应用程序源:
https://github.com/TheMasterBaron/Toggle-2G

All the other answers are correct that this requires access to Settings.Secure. Take a look at how the phone app handles this setting https://github.com/dzo/packages_apps_phone/blob/master/src/com/android/phone/Use2GOnlyCheckBoxPreference.java

or take a look at the Toggle2G app source:
https://github.com/TheMasterBaron/Toggle-2G

|煩躁 2024-12-29 10:27:26

http://developer.android.com/reference/android/provider/Settings .System.html

除了在 Activity.java 中编写代码之外,您可能还需要请求权限才能访问 AndroidManifest.xml 中的这些设置。所以这很烦人,但应该足够简单。

http://developer.android.com/reference/android/provider/Settings.System.html

Aside from writing the code in your Activity.java, you will probably have to ask for permission to access these settings in the AndroidManifest.xml. So it's annoying but should be simple enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文