Android 移动数据网络连接管理
发布网友
发布时间:2022-05-30 21:15
我来回答
共1个回答
热心网友
时间:2023-11-18 23:13
真机小米1s测试可用
public static void setDataConnectionState(Context cxt, boolean state) {
ConnectivityManager connectivityManager = null;
Class connectivityManagerClz = null;
try {
connectivityManager = (ConnectivityManager) cxt
.getSystemService("connectivity");
connectivityManagerClz = connectivityManager.getClass();
Method method = connectivityManagerClz.getMethod(
"setMobileDataEnabled", new Class[] { boolean.class });
method.invoke(connectivityManager, state);
} catch (Exception e) {
e.printStackTrace();
}
}
加上权限:
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
参考资料:http://my.oschina.net/wisedream/blog/198466追问可是不能关闭网络呢?你那儿可以么