更改移动网络模式(gsm,wcdma,auto)

Moh*_*ikh 12 android telephony android-networking

我想改变首选的网络模式,即.在Android上以编程方式从代码中获取gsm或wcdma或auto.

这是可能的,如果是这样的话怎么样?

Tol*_*lio 8

有可能,我做到了.

为此,您的应用必须使用系统密钥签名或拥有运营商权限.否则应用程序将抛出 java.lang.SecurityException: No modify permission or carrier privilege.

我的应用程序在Android 5.1 Lollipop(API 22)上运行,并使用系统密钥进行签名,因此这是我可以确认的唯一配置.我无法确认运营商权限方法.

AndroidManifest.xml中

将此权限添加到您的应用清单中.如果您使用的是Android Studio,则可能会将此行标记为错误,因为只有系统应用才能拥有此权限.如果您可以使用系统密钥对应用进行签名,请不要担心.

<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
Run Code Online (Sandbox Code Playgroud)

获得首选网络

返回在RILConstants.java中定义,例如 RILConstants.NETWORK_MODE_WCDMA_PREF

public int getPreferredNetwork() {
    Method method = getHiddenMethod("getPreferredNetworkType", TelephonyManager.class, null);
    int preferredNetwork = -1000;
    try {
        preferredNetwork = (int) method.invoke(mTelephonyManager);
        Log.i(TAG, "Preferred Network is ::: " + preferredNetwork);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

    return preferredNetwork;
}
Run Code Online (Sandbox Code Playgroud)

设置首选方法.

参数必须基于RILConstants.java,例如:RILConstants.NETWORK_MODE_LTE_ONLY

public void setPreferredNetwork(int networkType) {
    try {
        Method setPreferredNetwork = getHiddenMethod("setPreferredNetworkType",
                TelephonyManager.class, new Class[] {int.class});
        Boolean success = (Boolean)setPreferredNetwork.invoke(mTelephonyManager,
                networkType);
        Log.i(TAG, "Could set Network Type ::: " + (success.booleanValue() ? "YES" : "NO"));
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一种访问隐藏API方法的实用方法.

/**
 * Get a hidden method instance from a class
 * @param methodName The name of the method to be taken from the class
 * @param fromClass The name of the class that has the method
 * @return A Method instance that can be invoked
 */
public Method getHiddenMethod(String methodName, Class fromClass, Class[] params) {
    Method method = null;
    try {
        Class clazz = Class.forName(fromClass.getName());
        method = clazz.getMethod(methodName, params);
        method.setAccessible(true);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }

    return method;
}
Run Code Online (Sandbox Code Playgroud)


MKJ*_*ekh 3

答案是否定的

我们可以直接打开移动网络设置的设置应用程序来切换“2G”和“允许3G”网络。遗憾的是无法直接切换。

我们可以开发一些东西来显示当前的网络,并允许用户通过应用程序快捷方式切换网络。

  • 我不能,但您应该逆向工程默认设置应用程序,将您的应用程序安装到 /system/app/ 并将 WRITE_SECURE_SETTINGS 添加到使用权限。我不知道为什么,但如果你觉得有趣=) (2认同)