我正在为android开发一个软件,我想自动打开用户的互联网连接。像这些互联网开/关小部件一样。
答案 0 :(得分:2)
private void setMobileDataEnabled(Context context, boolean enabled) throws Exception{
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Class conmanClass = null;
try {
conmanClass = Class.forName(conman.getClass().getName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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);
}
这是3g激活类似于wifi
答案 1 :(得分:0)
您可以使用WifiManager调用setWifiEnabled
这里有更多细节:
http://developer.android.com/reference/android/net/wifi/WifiManager.html