1、Android如何使用代码判断是否Internet连接正常。
android代码如下:
- public static boolean hasInternet(Activity activity) {
- ConnectivityManager manager = (ConnectivityManager) activity
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo info = manager.getActiveNetworkInfo();
- if (info == null || !info.isConnected()) {
- return false;
- }
- if (info.isRoaming()) {
- // here is the roaming option you can change it if you want to
- // disable internet while roaming, just return false
- return true;
- }
- return true;
- }
需要在AndroidManifest.xml中增加权限:
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
使用纯java的话可以这么试试:
- boolean networkReachable = false;
- try {
- networkReachable = InetAddress.getByName("www.google.com").isReachable(1000); //超时1000ms
- } catch (java.net.UnknownHostException ex) {
- }
- System.out.println(networkReachable);
2、3g、wifi状态:
- ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- //mobile 3G Data Network
- State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
- txt3G.setText(mobile.toString()); //显示3G网络连接状态
- //wifi
- State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
- txtWifi.setText(wifi.toString());
- //显示wifi连接状态
3、ConnectivityManager和NetworkInfo 说明:
- //获取网络连接管理者
- ConnectivityManager connectionManager = (ConnectivityManager)
- getSystemService(CONNECTIVITY_SERVICE);
- //获取网络的状态信息,有下面三种方式
- NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
- NetworkInfo 有一下方法
- getDetailedState():获取详细状态。
- getExtraInfo():获取附加信息。
- getReason():获取连接失败的原因。
- getType():获取网络类型(一般为移动或Wi-Fi)。
- getTypeName():获取网络类型名称(一般取值“WIFI”或“MOBILE”)。
- isAvailable():判断该网络是否可用。
- isConnected():判断是否已经连接。
- isConnectedOrConnecting():判断是否已经连接或正在连接。
- isFailover():判断是否连接失败。
- isRoaming():判断是否漫游
4、得到联网方式的方法
- public String NetType(Context context) {
- try {
- ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo info = cm.getActiveNetworkInfo();
- String typeName = info.getTypeName().toLowerCase; // WIFI/MOBILE
- if(typeName.equals.("wifi")){
- }else{
- typeName = mActiveNetworkInfo.getExtraInfo().toLowerCase();
- //3gnet/3gwap/uninet/uniwap/cmnet/cmwap/ctnet/ctwap
- }
- return typeName;
- } catch (Exception e) {
- return null;
- }
- }
- public String NetType(Context context) {
- try {
- ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo info = cm.getActiveNetworkInfo();
- String typeName = info.getTypeName().toLowerCase; // WIFI/MOBILE
- if(typeName.equals.("wifi")){
- }else{
- typeName = mActiveNetworkInfo.getExtraInfo().toLowerCase();
- //3gnet/3gwap/uninet/uniwap/cmnet/cmwap/ctnet/ctwap
- }
- return typeName;
- } catch (Exception e) {
- return null;
- }
- }
- 没有网络时会出现异常,位置为ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo info = cm.getActiveNetworkInfo();
5、使用代理联网时得到连接对象的方法:
- private HttpURLConnection getURLConnection(String url) throws Exception {
- String proxyHost = android.net.Proxy.getDefaultHost();
- if (proxyHost != null) {
- java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP,
- new InetSocketAddress(android.net.Proxy.getDefaultHost(),
- android.net.Proxy.getDefaultPort()));
- return (HttpURLConnection) new URL(url).openConnection(p);
- } else {
- return (HttpURLConnection) new URL(url).openConnection();
- }
- }
- private HttpURLConnection getURLConnection(String url) throws Exception {
- String proxyHost = android.net.Proxy.getDefaultHost();
- if (proxyHost != null) {
- java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP,
- new InetSocketAddress(android.net.Proxy.getDefaultHost(),
- android.net.Proxy.getDefaultPort()));
- return (HttpURLConnection) new URL(url).openConnection(p);
- } else {
- return (HttpURLConnection) new URL(url).openConnection();
- }
- }
返回HttpURLConnection对象android.net.Proxy.getDefaultHost()得到手机设置的代理ip,得到android.net.Proxy.getDefaultPort()得到手机设置的端口;
也可以自己设置为 10.0.0.172 端口 80