在ANDROID智能手机上网络模式联网
2011-05-17 14:45
351 查看
在ANDROID智能手机上,支持的网络模式很多,有WIFI、移动网络,而移动网络又分走不走网关,如果是双卡双待的手机,则更为复杂。那么在程序中如何进行正确的网络连接呢?以下是我在项目中所采用的方式。
01.// 说明:联网时优先选择WIFI联网,如果WIFI没开或不可用,则使用移动网络
02.String result = "";
03.HttpURLConnection httpURLConn;
04.try {
05.
06. // 获取当前可用网络信息
07. ConnectivityManager connMng = (ConnectivityManager) mContext
08. .getSystemService(Context.CONNECTIVITY_SERVICE);
09. NetworkInfo netInf = connMng.getActiveNetworkInfo();
10.
11. // 如果当前是WIFI连接
12. if (netInf != null && NET_TYPE_WIFI.equals(netInf.getTypeName())) {
13. httpURLConn = (HttpURLConnection) new URL(mReqUrl)
14. .openConnection();
15. }
16. // 非WIFI联网
17. else {
18. String proxyHost = android.net.Proxy.getDefaultHost();
19. MyLog.i(TAG, "proxyHost : " + proxyHost);
20.
21. // 代理模式
22. if (proxyHost != null) {
23. java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(android.net.Proxy.getDefaultHost(),android.net.Proxy.getDefaultPort()));
24.
25. httpURLConn = (HttpURLConnection) new URL(mReqUrl).openConnection(p);
26.
27. }
28.
29. // 直连模式
30. else {
31. httpURLConn = (HttpURLConnection) new URL(mReqUrl).openConnection();
32. }
33. }
// 说明:联网时优先选择WIFI联网,如果WIFI没开或不可用,则使用移动网络
String result = "";
HttpURLConnection httpURLConn;
try {
// 获取当前可用网络信息
ConnectivityManager connMng = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInf = connMng.getActiveNetworkInfo();
// 如果当前是WIFI连接
if (netInf != null && NET_TYPE_WIFI.equals(netInf.getTypeName())) {
httpURLConn = (HttpURLConnection) new URL(mReqUrl)
.openConnection();
}
// 非WIFI联网
else {
String proxyHost = android.net.Proxy.getDefaultHost();
MyLog.i(TAG, "proxyHost : " + proxyHost);
// 代理模式
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()));
httpURLConn = (HttpURLConnection) new URL(mReqUrl).openConnection(p);
}
// 直连模式
else {
httpURLConn = (HttpURLConnection) new URL(mReqUrl).openConnection();
}
}
ANDROID提供了查询当前有效网络信息的API,即系统服务CONNECTIVITY_SERVICE,在使用时需要在AndroidManifest.xml文件中声明权限。根据该API在上述代码中可以查询到当前使用的WIFI还是MOBILE。
1、如果是WIFI,则直接创建HttpURLConnection对象,进行网络连接;
2、如果是MOBILE或者getActiveNetworkInfo()为空,则按代码中非WIFI联网的代码进行处理。这里通过Proxy.getDefaultHost()查看是否代理模式。
使用这种方式避免了在程序中去做wap、net的判断
01.// 说明:联网时优先选择WIFI联网,如果WIFI没开或不可用,则使用移动网络
02.String result = "";
03.HttpURLConnection httpURLConn;
04.try {
05.
06. // 获取当前可用网络信息
07. ConnectivityManager connMng = (ConnectivityManager) mContext
08. .getSystemService(Context.CONNECTIVITY_SERVICE);
09. NetworkInfo netInf = connMng.getActiveNetworkInfo();
10.
11. // 如果当前是WIFI连接
12. if (netInf != null && NET_TYPE_WIFI.equals(netInf.getTypeName())) {
13. httpURLConn = (HttpURLConnection) new URL(mReqUrl)
14. .openConnection();
15. }
16. // 非WIFI联网
17. else {
18. String proxyHost = android.net.Proxy.getDefaultHost();
19. MyLog.i(TAG, "proxyHost : " + proxyHost);
20.
21. // 代理模式
22. if (proxyHost != null) {
23. java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(android.net.Proxy.getDefaultHost(),android.net.Proxy.getDefaultPort()));
24.
25. httpURLConn = (HttpURLConnection) new URL(mReqUrl).openConnection(p);
26.
27. }
28.
29. // 直连模式
30. else {
31. httpURLConn = (HttpURLConnection) new URL(mReqUrl).openConnection();
32. }
33. }
// 说明:联网时优先选择WIFI联网,如果WIFI没开或不可用,则使用移动网络
String result = "";
HttpURLConnection httpURLConn;
try {
// 获取当前可用网络信息
ConnectivityManager connMng = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInf = connMng.getActiveNetworkInfo();
// 如果当前是WIFI连接
if (netInf != null && NET_TYPE_WIFI.equals(netInf.getTypeName())) {
httpURLConn = (HttpURLConnection) new URL(mReqUrl)
.openConnection();
}
// 非WIFI联网
else {
String proxyHost = android.net.Proxy.getDefaultHost();
MyLog.i(TAG, "proxyHost : " + proxyHost);
// 代理模式
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()));
httpURLConn = (HttpURLConnection) new URL(mReqUrl).openConnection(p);
}
// 直连模式
else {
httpURLConn = (HttpURLConnection) new URL(mReqUrl).openConnection();
}
}
ANDROID提供了查询当前有效网络信息的API,即系统服务CONNECTIVITY_SERVICE,在使用时需要在AndroidManifest.xml文件中声明权限。根据该API在上述代码中可以查询到当前使用的WIFI还是MOBILE。
1、如果是WIFI,则直接创建HttpURLConnection对象,进行网络连接;
2、如果是MOBILE或者getActiveNetworkInfo()为空,则按代码中非WIFI联网的代码进行处理。这里通过Proxy.getDefaultHost()查看是否代理模式。
使用这种方式避免了在程序中去做wap、net的判断
相关文章推荐
- 智能手机进入开发者模式 小米2A 操作系统Android 4.4 开发者模式
- android 手机网络接入点名称及WAP、NET模式的区别
- Android智能手机网络防火墙开发的经验心得
- Android智能手机网络防火墙开发的经验心得
- ANDROID下多种网络模式的联网处理
- 智能手机软件平台 Android VS iPhone OS: 商业模式分析 (2/4)
- WireX:Android智能手机组成的DDoS僵尸网络
- Android中手机的网络状态(判断是否联网/3G/4G/WIFI)
- android 网络连接了wifi,但是并没有网络;或者手机联网了 ,APP检测显示未联网
- 如何获取Android手机连接当前网络的外网IP
- android 手机链接3g网络的问题
- 2013年android智能手机599元大战开战
- WireX:Android智能手机组成的DDoS僵尸网络
- Android智能手机上的音频浅析
- android如何判断当前网络类型(联网,2g,3g,wifi等)
- pyserial应用实例——对工厂模式下的Android手机收发AT命令
- Uart2Socket:将PC的串口信息通过WiFi网络传输至安卓Android手机上
- Android程序:获取当前手机的网络类型
- 【转】通过UserAgent判断智能手机(设备,Android,IOS)