如何关闭3G数据连接编程方式在android的? [英] How to turn off 3G data connection programmatically in android?
问题描述
欲启用/禁用3G等数据连接separately.Is能够分别访问3G?这意味着如果用户选择的选项3G,然后启用only.And如果用户选择选项/禁用3G数据连接数据服务,启用/禁用数据连接,除了3G.Is这可能吗?
我尝试以下方法
enableDataConnectivity();
和
disableDataConnectivity();
的的
TelephonyManager
I want to Enable/Disable 3G and other Data connections separately.Is it possible to access 3G individually?That means if the user selects the option 3G, then Enable/Disable 3G data connection only.And if the user selects the option Data Services, enable/disable data connections except 3G.Is it possible?
I tried the following methods
enableDataConnectivity();
and
disableDataConnectivity();
of
TelephonyManager
但我认为这些方法whole.I关闭/开启所有的数据业务要分别接入3G。
But i think these methods turn off/on all data services in whole.I want to access 3G separately.
如何做到这一点?
推荐答案
这code样品应适用于Android手机上运行姜饼和更高的:
This code sample should work for android phones running gingerbread and higher:
private void setMobileDataEnabled(Context context, boolean enabled)
{
final ConnectivityManager conman = (ConnectivityManager)
final ConnectivityManager conman = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}
这还需要以下权限。
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
这篇关于如何关闭3G数据连接编程方式在android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!