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