Wifi,cmwap,cmnet 链接自动切换
程序员文章站
2022-04-04 12:13:58
...
思路:
首先判断是Wifi还是Mobile,如果是Mobile 有两种,一种是cmwap,另一种是cmnet。
如果是cmwap ,则需要设置代理才能连接。
代码如下:
ConnectivityManager conManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile 3G Data Network State
mobile = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
//wifi State
wifi = conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
取得网络链接
urlConn = (HttpURLConnection)url.openConnection(); //Wifi
//-----------cmwap-----------
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.172",80));
urlConn = (HttpURLConnection)url.openConnection(proxy);