android开发中有时候需要设定程序连接网络优先 2G/3G 或 WIFI,下面片段代码给出设置方法:
public void setPreferredNetwork(int networkType) { ConnectivityManager connMgr = (ConnectivityManager) this.context.getSystemService("connectivity"); if (networkType == NetworkType.TYPE_MOBILE) {//设为2G/3G网络优先,就算wifi连接到AP,系统仍然通过2G/3G访问网络 connMgr.setNetworkPreference(0); } else if (networkType == NetworkType.TYPE_WIFI) { connMgr.setNetworkPreference(1); } WifiManager wifiMgr = (WifiManager) this.context.getSystemService("wifi"); wifiMgr.disconnect(); }