- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想更改首选网络模式,即。 gsm 或 wcdma 或自动,以编程方式,从代码,在 Android 上。
这可能吗,如果可能的话怎么办?
最佳答案
为此,您的应用必须使用系统 key 签名或具有运营商权限。否则应用程序将抛出java.lang.SecurityException:没有修改权限或运营商特权。
我的应用程序在 Android 5.1 Lollipop(API 22) 上运行并使用系统 key 签名,因此这是我可以确认有效的唯一配置。我无法确认运营商特权方法。
将此权限添加到您的应用 list 。如果您使用的是 Android Studio,它可能会将此行标记为错误,因为只有系统应用程序才能拥有此权限。如果您可以使用系统 key 为您的应用签名,请不要担心。
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
返回在 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;
}
参数必须基于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();
}
}
这是一个访问隐藏 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;
}
关于android - 更改移动网络模式(gsm、wcdma、自动),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10170179/
您好,我正在开发一个 Android 应用程序来监控 WCDMA 网络的 RSCP 和 ECNO,我尝试使用以下代码,在构建过程中没有错误,但当我在手机上运行该应用程序时出现错误(Samsung Ga
我想更改首选网络模式,即。 gsm 或 wcdma 或自动,以编程方式,从代码,在 Android 上。 这可能吗,如果可能的话怎么办? 最佳答案 有可能,我做到了。 为此,您的应用必须使用系统 ke
我正在使用谷歌地图地理定位 API。其中有一个名为 radioType 的字段,唯一支持的值是 GSM、CDMA、WCDMA 和 LTE。尽管这些字段是可选的,但我将其包含在内是为了获得更准确的结果。
我有一个在 Android 1.6+ 版本上运行的应用程序,需要检测网络类型。 不过,我遇到了 HSDPA 和 WCDMA 的问题 - 前者在 API 版本 5+ 中只有 TelephonyManag
我正在使用下面的 adb 命令将首选网络模式更改为 GSM,其中值为 1,任何人都可以给出其他模式的值,例如- GWL(仅 GSM/WCDMA/LTE)- 仅限 LTE- WCDMA 首选等 adb
我正在创建一个使用 Google 的 Geolocation API 的应用程序,如文档所述 here . 作为我需要发送到他们的 API 的 POST 请求的一部分,我需要包括移动 radio 类型
我是一名优秀的程序员,十分优秀!