void changeToNetAPN(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo != null) { String extra = activeNetInfo.getExtraInfo() == null ? "" : activeNetInfo.getExtraInfo(); if (extra.toLowerCase().contains("wap")) { try { NetCheck.checkNetworkInfo(context); } catch (Exception e) { e.printStackTrace(); } } } }
/** * 获取当前网络类型 * * @return 0:没有网络 1:WIFI网络 2:WAP网络 3:NET网络 */ public int getNetworkType() { int netType = NETTYPE_UNAVAILABLE; ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo == null) { return netType; } int nType = networkInfo.getType(); if (nType == ConnectivityManager.TYPE_MOBILE) { String extraInfo = networkInfo.getExtraInfo(); if (!StringUtils.isEmpty(extraInfo)) { if ("cmnet".equals(extraInfo.toLowerCase(Locale.getDefault()))) { netType = NETTYPE_CMNET; } else { netType = NETTYPE_CMWAP; } } } else if (nType == ConnectivityManager.TYPE_WIFI) { netType = NETTYPE_WIFI; } return netType; }
public static HttpURLConnection getConnection(String urlStr) throws IOException { URL url = new URL(urlStr); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); if (null == activeNetworkInfo) { Log.w(TAG, "No active network available!"); return null; } Proxy p = getProxy(); String extra = activeNetworkInfo.getExtraInfo(); // chinese mobile network if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE && p != null) { // cmwap, uniwap, 3gwap if (extra != null && (extra.startsWith("cmwap") || extra.startsWith("uniwap") || extra.startsWith("3gwap"))) { HttpURLConnection conn = (HttpURLConnection) new URL("http://" + p.address().toString() + url.getPath()).openConnection(); conn.setRequestProperty( "X-Online-Host", url.getHost() + ":" + (url.getPort() == -1 ? "80" : url.getPort())); return conn; } } if (null != p) { // through proxy return (HttpURLConnection) url.openConnection(p); } else { return (HttpURLConnection) url.openConnection(); } }
/** * 取得URL * * @param context * @param getRequest * @return request URL */ public String getRequestUrl(Context context, HttpPost getRequest) { final StringBuilder urlBuilder = new StringBuilder(); ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkinfo = connManager.getActiveNetworkInfo(); String extraInfo = null; if (networkinfo != null) { extraInfo = networkinfo.getExtraInfo(); } if (extraInfo != null && (extraInfo.equals(CMWAP) || extraInfo.equals(CTWAP) || extraInfo.equals(UNIWAP) || extraInfo.equals(GWAP))) { urlBuilder.append(X_OFFLINE_HOST); isWapState = true; } else { urlBuilder.append(SCHEME); urlBuilder.append(getRequest.getURI().getHost()); int port = getRequest.getURI().getPort(); if (port != -1) { urlBuilder.append(PORT_TOKEN); urlBuilder.append(port); } } urlBuilder.append(getRequest.getURI().getPath()); final String query = getRequest.getURI().getQuery(); if (!TextUtils.isEmpty(query)) { urlBuilder.append("?"); urlBuilder.append(query); } return urlBuilder.toString(); }
/** * Get the latest network connection information * * @param info the current active network info * @return a JSONObject that represents the network info */ private JSONObject getConnectionInfo(NetworkInfo info) { String type = TYPE_NONE; String extraInfo = ""; if (info != null) { // If we are not connected to any network set type to none if (!info.isConnected()) { type = TYPE_NONE; } else { type = getType(info); } extraInfo = info.getExtraInfo(); } Log.d("CordovaNetworkManager", "Connection Type: " + type); Log.d("CordovaNetworkManager", "Connection Extra Info: " + extraInfo); JSONObject connectionInfo = new JSONObject(); try { connectionInfo.put("type", type); connectionInfo.put("extraInfo", extraInfo); } catch (JSONException e) { } return connectionInfo; }
/** * 获取网络连接信息</br> 无网络:</br> WIFI网络:WIFI</br> WAP网络:CMWAP</br> NET网络:CMNET</br> * * @return */ public String getNetworkType() { if (isConnected()) { int type = networkInfo.getType(); if (ConnectivityManager.TYPE_MOBILE == type) { Log.i(TAG, "networkInfo.getExtraInfo()-->" + networkInfo.getExtraInfo()); if (NETWORK_CMWAP.equals(networkInfo.getExtraInfo().toLowerCase())) { return NETWORK_CMWAP; } else { return NETWORK_CMNET; } } else if (ConnectivityManager.TYPE_WIFI == type) { return NETWORK_WIFI; } } return ""; }
/** * Cmwap网络是否已连接 * * @param context * @return */ public static boolean isNetworkConnectedByCmwap(Context context) { NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)) .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); return networkInfo != null && networkInfo.isConnected() && networkInfo.getExtraInfo() != null && networkInfo.getExtraInfo().toLowerCase().contains("cmwap"); }
/** * 检测网络连接是否是cmwap 如果是的话需要设置代理 * * @param context * @return */ public static boolean isWap(Context context) { // ConnectivityManager主要管理和网络连接相关的操作 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo nInfo = cm.getActiveNetworkInfo(); if (nInfo == null || nInfo.getType() != ConnectivityManager.TYPE_MOBILE) return false; String extraInfo = nInfo.getExtraInfo(); if (extraInfo == null || extraInfo.length() < 3) return false; if (extraInfo.toLowerCase(Locale.getDefault()).contains("wap")) return true; return false; }
// 充值 private void recharge(final RechargePojo rechargepojo) { RWSharedPreferences pre = new RWSharedPreferences(AlipaySecureActivity.this, "addInfo"); sessionId = pre.getStringValue(ShellRWConstants.SESSIONID); phonenum = pre.getStringValue(ShellRWConstants.MOBILEID); userno = pre.getStringValue(ShellRWConstants.USERNO); ConnectivityManager ConnMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = ConnMgr.getActiveNetworkInfo(); if (info.getExtraInfo() != null && info.getExtraInfo().equalsIgnoreCase("3gwap")) { Toast.makeText(this, "提醒:检测到您的接入点为3gwap,可能无法正确充值,请切换到3gnet!", Toast.LENGTH_LONG).show(); } showDialog(0); new Thread( new Runnable() { @Override public void run() { String error_code = "00"; message = ""; // TODO Auto-generated method stub try { rechargepojo.setSessionid(sessionId); rechargepojo.setUserno(userno); String re = RechargeInterface.getInstance().recharge(rechargepojo); JSONObject obj = new JSONObject(re); error_code = obj.getString("error_code"); message = obj.getString("message"); if (error_code.equals("0000")) { url = obj.getString("return_url"); } } catch (JSONException e) { e.printStackTrace(); } handler.handleMsg(error_code, message); progressdialog.dismiss(); } }) .start(); }
private String getApnName() { try { ConnectivityManager conManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = conManager.getActiveNetworkInfo(); if (null == ni) { return null; } return ni.getExtraInfo(); } catch (Exception e) { LogUtils.e("failed to get apn name", e); return null; } }
public static synchronized HttpClient getInstance(Context context) { if (null == customerHttpClient) { HttpParams params = new BasicHttpParams(); // 设置一些基本参数 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, CHARSET); HttpProtocolParams.setUseExpectContinue(params, true); HttpProtocolParams.setUserAgent( params, System.getProperties().getProperty("http.agent") + " Mozilla/5.0 Firefox/26.0"); // 超时设置 /* 从连接池中取连接的超时时间 */ ConnManagerParams.setTimeout(params, 10 * 1000); /* 连接超时 */ HttpConnectionParams.setConnectionTimeout(params, 10 * 1000); /* 请求超时 */ HttpConnectionParams.setSoTimeout(params, 10 * 1000); // 支持http和https两种模式 SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schReg.register(new Scheme("https", getSSLSocketFactory(), 443)); // 使用线程安全的连接管理来创建HttpClient ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg); customerHttpClient = new DefaultHttpClient(conMgr, params); customerHttpClient.setHttpRequestRetryHandler(requestRetryHandler); ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkinfo = manager.getActiveNetworkInfo(); String net = networkinfo != null ? networkinfo.getExtraInfo() : null; // wifi的值为空 if (!TextUtils.isEmpty(net)) { String proxyHost = getDefaultHost(); if (!TextUtils.isEmpty(proxyHost)) { HttpHost proxy = new HttpHost(proxyHost, getDefaultPort(), "http"); customerHttpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } } } return customerHttpClient; }
public static boolean isNetWorkAvailable(Context context) { boolean isAvailable = false; ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager != null) { NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo != null) { DetailedState detailedState = networkInfo.getDetailedState(); String detailedName = detailedState.name(); if (detailedName == null) { detailedName = ""; } networkTable.put("detailedName", detailedName); String extraInfo = networkInfo.getExtraInfo(); if (extraInfo == null) { extraInfo = ""; } networkTable.put("extraInfo", extraInfo); String reason = networkInfo.getReason(); if (reason == null) { reason = ""; } networkTable.put("reason", reason); NetworkInfo.State state = networkInfo.getState(); String stateName = ""; if (state != null && state.name() != null) { stateName = state.name(); } // 经过多次测试,只有stateName可以准确的判断网络连接是否正常 if ("CONNECTED".equalsIgnoreCase(stateName)) { isAvailable = true; } networkTable.put("stateName", stateName); int subType = networkInfo.getSubtype(); networkTable.put("subType", subType + ""); String subtypeName = networkInfo.getSubtypeName(); if (subtypeName == null) { subtypeName = ""; } networkTable.put("subtypeName", subtypeName); // int type = networkInfo.getType(); String typeName = networkInfo.getTypeName(); if (typeName == null) { typeName = ""; } networkTable.put("typeName", typeName); Log.d(LOG_TAG, getLogString()); } } return isAvailable; }
/** * @param context * @param getRequest * @return request URL */ public static boolean isWap(Context context) { ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkinfo = connManager.getActiveNetworkInfo(); String extraInfo = null; if (networkinfo != null) { extraInfo = networkinfo.getExtraInfo(); } if (extraInfo != null && (extraInfo.equals(CMWAP) || extraInfo.equals(UNIWAP) || extraInfo.equals(CTWAP))) { return true; } return false; }
/** * @param context * @return * <p>获取当前的网络状态 -1:没有网络 1:WIFI网络2:wap 网络3:net网络 */ public static netType getAPNType(Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo == null) { return netType.noneNet; } int nType = networkInfo.getType(); if (nType == ConnectivityManager.TYPE_MOBILE) { if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) { return netType.CMNET; } else { return netType.CMWAP; } } else if (nType == ConnectivityManager.TYPE_WIFI) { return netType.wifi; } return netType.noneNet; }
/** * 获取当前网络类型 * * @return 0:没有网络 1:WIFI网络 2:WAP网络 3:NET网络 */ public int getNetworkType() { int netType = 0; NetworkInfo networkInfo = NetworkStatusManager.getInstance().getNetworkInfo(); if (networkInfo == null) { return netType; } int nType = networkInfo.getType(); if (nType == ConnectivityManager.TYPE_MOBILE) { String extraInfo = networkInfo.getExtraInfo(); if (!StringUtil.isEmpty(extraInfo)) { if (extraInfo.toLowerCase().equals("cmnet")) { netType = NETTYPE_CMNET; } else { netType = NETTYPE_CMWAP; } } } else if (nType == ConnectivityManager.TYPE_WIFI) { netType = NETTYPE_WIFI; } return netType; }
public static String getAPN(Context context) { String apn = ""; ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); if (info != null) { if (ConnectivityManager.TYPE_WIFI == info.getType()) { apn = info.getTypeName(); if (apn == null) { apn = "wifi"; } } else { apn = info.getExtraInfo().toLowerCase(); if (apn == null) { apn = "mobile"; } } } return apn; }
private void handleUpdateNetworkState(int state, NetworkInfo info) { mNetworkAvailable = (state == LocationProvider.AVAILABLE); if (DEBUG) { Log.d( TAG, "updateNetworkState " + (mNetworkAvailable ? "available" : "unavailable") + " info: " + info); } if (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE_SUPL && mAGpsDataConnectionState == AGPS_DATA_CONNECTION_OPENING) { String apnName = info.getExtraInfo(); if (mNetworkAvailable && apnName != null && apnName.length() > 0) { mAGpsApn = apnName; if (DEBUG) Log.d(TAG, "call native_agps_data_conn_open"); native_agps_data_conn_open(apnName); mAGpsDataConnectionState = AGPS_DATA_CONNECTION_OPEN; } else { if (DEBUG) Log.d(TAG, "call native_agps_data_conn_failed"); mAGpsApn = null; mAGpsDataConnectionState = AGPS_DATA_CONNECTION_CLOSED; native_agps_data_conn_failed(); } } if (mNetworkAvailable) { if (mInjectNtpTimePending) { mHandler.removeMessages(INJECT_NTP_TIME); mHandler.sendMessage(Message.obtain(mHandler, INJECT_NTP_TIME)); } if (mDownloadXtraDataPending) { mHandler.removeMessages(DOWNLOAD_XTRA_DATA); mHandler.sendMessage(Message.obtain(mHandler, DOWNLOAD_XTRA_DATA)); } } }
private String getConnectivity() { ConnectivityManager conn = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE); String result = "<table>"; result += "<tr>"; result += "<th>DetailedState</th>"; result += "<th>ExtraInfo</th>"; result += "<th>Reason</th>"; result += "<th>State</th>"; result += "<th>Subtype</th>"; result += "<th>Subtype name</th>"; result += "<th>Type</th>"; result += "<th>Type name</th>"; result += "<th>Is connected</th>"; result += "<th>Is connected or connecting</th>"; result += "<th>Is failover</th>"; result += "<th>Is roaming</th>"; result += "</tr>\n"; for (NetworkInfo info : conn.getAllNetworkInfo()) { result += "<tr>"; result += "<td>" + info.getDetailedState() + "</td>"; result += "<td>" + info.getExtraInfo() + "</td>"; result += "<td>" + info.getReason() + "</td>"; result += "<td>" + info.getState() + "</td>"; result += "<td>" + info.getSubtype() + "</td>"; result += "<td>" + info.getSubtypeName() + "</td>"; result += "<td>" + info.getType() + "</td>"; result += "<td>" + info.getTypeName() + "</td>"; result += "<td>" + info.isConnected() + "</td>"; result += "<td>" + info.isConnectedOrConnecting() + "</td>"; result += "<td>" + info.isFailover() + "</td>"; result += "<td>" + info.isRoaming() + "</td>"; result += "</tr>\n"; } result += "</table>"; return result; }
public int getNetworkType() { int netType = 0; ConnectivityManager connectivityManager = (ConnectivityManager) this.mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo == null) { return netType; } int nType = networkInfo.getType(); if (nType == ConnectivityManager.TYPE_MOBILE) { String extraInfo = networkInfo.getExtraInfo(); if (extraInfo != "" && !extraInfo.equals("")) { if (extraInfo.toLowerCase().equals("cmnet")) { netType = NETTYPE_CMNET; } else { netType = NETTYPE_CMWAP; } } } else if (nType == ConnectivityManager.TYPE_WIFI) { netType = NETTYPE_WIFI; } return netType; }
/** * is current connected network is cmwap * * @return */ public static boolean isNetworkCmwap(Context ctx) { // 以下为支持cmwap部分,移动和联通的cmwap代理貌似都是10.0.0.172,有待测试 String proxyHost = android.net.Proxy.getDefaultHost(); // 判断网络状况,如果有其它通路的话就不要再使用cmwap的代理 try { if (proxyHost != null && !isNetworkWifi(ctx)) { ConnectivityManager manager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); String extraInfo = null; if (info != null) extraInfo = info.getExtraInfo(); if (extraInfo != null) { String extraTrim = extraInfo.trim().toLowerCase(); if (extraTrim.equals("cmwap") || extraTrim.equals("uniwap") || extraTrim.equals("ctnet") || extraTrim.equals("ctwap")) return true; } } } catch (Exception e) { } return false; }
@SuppressLint("DefaultLocale") private HttpHost getProxy() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); HttpHost none_host = null; if (cm == null) { return none_host; } NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni == null) { return none_host; } if (ni.getType() == ConnectivityManager.TYPE_WIFI) { return null; } else if (ni.getType() == ConnectivityManager.TYPE_MOBILE) { String extra = ni.getExtraInfo(); if (TextUtils.isEmpty(extra)) { return none_host; } extra = extra.toLowerCase(); if (extra.contains("cmnet") || extra.contains("ctnet") || extra.contains("uninet") || extra.contains("3gnet")) { return none_host; } else if (extra.contains("cmwap") || extra.contains("uniwap") || extra.contains("3gwap")) { return new HttpHost("10.0.0.172"); } else if (extra.contains("ctwap")) { return new HttpHost("10.0.0.200"); } } return none_host; }
public static int a(Context paramContext, JSONObject paramJSONObject, boolean paramBoolean) { String str1 = ac.a(2); if (!URLUtil.isHttpUrl(str1)) { x.f(); return -1; } HttpPost localHttpPost = new HttpPost(str1); BasicHttpParams localBasicHttpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(localBasicHttpParams, 30000); HttpConnectionParams.setSoTimeout(localBasicHttpParams, 30000); DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient(localBasicHttpParams); if (paramContext.getPackageManager().checkPermission(z[25], paramContext.getPackageName()) == 0) { NetworkInfo localNetworkInfo = ((ConnectivityManager)paramContext.getSystemService(z[30])).getActiveNetworkInfo(); if ((localNetworkInfo != null) && (localNetworkInfo.getType() != 1)) { String str5 = localNetworkInfo.getExtraInfo(); if ((str5 != null) && ((str5.equals(z[22])) || (str5.equals(z[19])) || (str5.equals(z[33])))) { localDefaultHttpClient.getParams().setParameter(z[34], new HttpHost(z[24], 80)); } } } String str2 = ""; if (paramJSONObject != null) { str2 = paramJSONObject.toString(); } if (ai.a(str2)) { x.c(); return -2; } localHttpPost.addHeader(z[28], z[31]); localHttpPost.addHeader(z[26], z[21]); localHttpPost.addHeader(z[35], z[21]); localHttpPost.addHeader(z[23], a.v(paramContext)); String str3; int i; if (str2 == null) { str3 = ac.a(paramContext); if (!ai.a(str3)) { break label343; } x.c(); i = 0; } for (;;) { if (i != 0) { break label405; } x.c(); return -3; str3 = ac.a(paramContext, str2, 2); break; label343: String str4 = ac.b(str3); if (ai.a(str4)) { x.c(); i = 0; } else { localHttpPost.addHeader(z[29], z[32] + str4); i = 1; } } try { label405: byte[] arrayOfByte1 = str2.getBytes(z[10]); ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream(); GZIPOutputStream localGZIPOutputStream = new GZIPOutputStream(localByteArrayOutputStream); localGZIPOutputStream.write(arrayOfByte1); localGZIPOutputStream.close(); byte[] arrayOfByte2 = localByteArrayOutputStream.toByteArray(); localByteArrayOutputStream.close(); ByteArrayEntity localByteArrayEntity = new ByteArrayEntity(arrayOfByte2); localByteArrayEntity.setContentEncoding(z[21]); localHttpPost.setEntity(localByteArrayEntity); try { int j = localDefaultHttpClient.execute(localHttpPost).getStatusLine().getStatusCode(); new StringBuilder(z[20]).append(j).toString(); x.c(); switch (j) { default: if (j / 100 == 5) { return 500; } case 401: x.e(); return 401; case 404: return 404; case 429: return 429; return -5; } } catch (ClientProtocolException localClientProtocolException) { new StringBuilder(z[27]).append(localClientProtocolException.getMessage()).toString(); x.f(); return -1; } catch (IOException localIOException2) { for (;;) { new StringBuilder(z[36]).append(localIOException2.getMessage()).toString(); x.f(); } } return 200; } catch (UnsupportedEncodingException localUnsupportedEncodingException)
/** * 判断Network具体类型(联通移动wap,电信wap,其他net) */ public static int checkNetworkType(Context mContext) { try { final ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo == null || !networkInfo.isAvailable()) { // 注意一: // NetworkInfo 为空或者不可以用的时候正常情况应该是当前没有可用网络, // 但是有些电信机器,仍可以正常联网, // 所以当成net网络处理依然尝试连接网络。 // (然后在socket中捕捉异常,进行二次判断与用户提示)。 Logs.i("", "=====================>无网络"); return TYPE_NET_WORK_DISABLED; } else { // NetworkInfo不为null开始判断是网络类型 int netType = networkInfo.getType(); if (netType == ConnectivityManager.TYPE_WIFI) { // wifi net处理 Logs.i("", "=====================>wifi网络"); return TYPE_WIFI_NET; } else if (netType == ConnectivityManager.TYPE_MOBILE) { // 注意二: // 判断是否电信wap: // 不要通过getExtraInfo获取接入点名称来判断类型, // 因为通过目前电信多种机型测试发现接入点名称大都为#777或者null, // 电信机器wap接入点中要比移动联通wap接入点多设置一个用户名和密码, // 所以可以通过这个进行判断! final Cursor c = mContext.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null); if (c != null) { c.moveToFirst(); final String user = c.getString(c.getColumnIndex("user")); if (!TextUtils.isEmpty(user)) { Logs.i("", "=====================>代理:" + c.getString(c.getColumnIndex("proxy"))); if (user.startsWith(CTWAP)) { Logs.i("", "=====================>电信wap网络"); return TYPE_CT_WAP; } } } c.close(); // 注意三: // 判断是移动联通wap: // 其实还有一种方法通过getString(c.getColumnIndex("proxy")获取代理ip // 来判断接入点,10.0.0.172就是移动联通wap,10.0.0.200就是电信wap,但在 // 实际开发中并不是所有机器都能获取到接入点代理信息,例如魅族M9 (2.2)等... // 所以采用getExtraInfo获取接入点名字进行判断 String netMode = networkInfo.getExtraInfo(); Logs.i("", "netMode ================== " + netMode); if (netMode != null) { // 通过apn名称判断是否是联通和移动wap netMode = netMode.toLowerCase(); if (netMode.equals(CMWAP) || netMode.equals(WAP_3G) || netMode.equals(UNIWAP)) { Logs.i("", "=====================>移动联通wap网络"); return TYPE_CM_CU_WAP; } } } } } catch (Exception ex) { ex.printStackTrace(); return TYPE_WIFI_NET; } return TYPE_WIFI_NET; }
public static String getNetWorkType(Context context) { try { final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo mobNetInfoActivity = connectivityManager.getActiveNetworkInfo(); if (mobNetInfoActivity == null || !mobNetInfoActivity.isAvailable()) { // 注意一: // NetworkInfo 为空或者不可以用的时候正常情况应该是当前没有可用网络, // 但是有些电信机器,仍可以正常联网, // 所以当成net网络处理依然尝试连接网络。 // (然后在socket中捕捉异常,进行二次判断与用户提示)。 return "2G"; } else { // NetworkInfo不为null开始判断是网络类型 int netType = mobNetInfoActivity.getType(); if (netType == ConnectivityManager.TYPE_WIFI) { // wifi net处理 return "wifi"; } else if (netType == ConnectivityManager.TYPE_MOBILE) { // 注意二: // 判断是否电信wap: // 不要通过getExtraInfo获取接入点名称来判断类型, // 因为通过目前电信多种机型测试发现接入点名称大都为#777或者null, // 电信机器wap接入点中要比移动联通wap接入点多设置一个用户名和密码, // 所以可以通过这个进行判断! boolean is3G = isFastMobileNetwork(context); final Cursor c = context.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null); if (c != null) { c.moveToFirst(); final String user = c.getString(c.getColumnIndex("user")); if (!TextUtils.isEmpty(user)) { if (user.startsWith(CTWAP)) { return is3G ? "3G" : "2G"; } else if (user.startsWith(CTNET)) { return is3G ? "3G" : "2G"; } } } c.close(); // 注意三: // 判断是移动联通wap: // 其实还有一种方法通过getString(c.getColumnIndex("proxy")获取代理ip // 来判断接入点,10.0.0.172就是移动联通wap,10.0.0.200就是电信wap,但在 // 实际开发中并不是所有机器都能获取到接入点代理信息,例如魅族M9 (2.2)等... // 所以采用getExtraInfo获取接入点名字进行判断 String netMode = mobNetInfoActivity.getExtraInfo(); Log.i("", "==================netmode:" + netMode); if (netMode != null) { // 通过apn名称判断是否是联通和移动wap netMode = netMode.toLowerCase(); if (netMode.equals(CMWAP)) { return is3G ? "3G" : "2G"; } else if (netMode.equals(CMNET)) { return is3G ? "3G" : "2G"; } else if (netMode.equals(NET_3G) || netMode.equals(UNINET)) { return is3G ? "3G" : "2G"; } else if (netMode.equals(WAP_3G) || netMode.equals(UNIWAP)) { return is3G ? "3G" : "2G"; } } } } } catch (Exception ex) { ex.printStackTrace(); return "2G"; } return "2G"; }
/** * 获得HttpClient * * @param context * @return HttpClient */ public static synchronized DefaultHttpClient getHttpClient(Context context) { mcontext = context; // registNetChangeBroadcast(); if (null == customerHttpClient || networkchanged) { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, CHARSET); HttpProtocolParams.setUseExpectContinue(params, true); // HttpProtocolParams.setUserAgent(params, USERAGENT); // ConnManagerParams.setTimeout(params, 15000); // HttpConnectionParams.setConnectionTimeout(params, 15000); HttpConnectionParams.setSoTimeout(params, 15000); SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg); customerHttpClient = new DefaultHttpClient(conMgr, params); // new DefaultHttpClient(); // customerHttpClient.getParams().setParameter("netmac", Utils.getMacAdress(mcontext)); customerHttpClient.getParams().setParameter("version", Utils.getVersion(mcontext)); // 添加证书 enableSSL(customerHttpClient); // String host=Proxy.getDefaultHost();//此处Proxy源自android.net // int port = Proxy.getDefaultPort();//同上 // Log.i(TAG, "-----------host="+host+"------port="+port); //Proxy:10.0.0.200 port:80 ConnectivityManager conManager = (ConnectivityManager) mcontext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); String currentAPN = info.getExtraInfo(); // Log.i(TAG, "----- ConnectivityManager ------currentAPN="+currentAPN); if (!isWifiConnected(mcontext)) { // 设置代理 if ("ctwap".equalsIgnoreCase(currentAPN)) { // 电信 HttpHost httpHost = new HttpHost("10.0.0.200", 80); customerHttpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, httpHost); Credentials defaultcreds = new UsernamePasswordCredentials("*****@*****.**", "vnet.mobi"); ((DefaultHttpClient) customerHttpClient) .getCredentialsProvider() .setCredentials(new AuthScope("10.0.0.200", 80, AuthScope.ANY_REALM), defaultcreds); } else if ("3gwap".equalsIgnoreCase(currentAPN)) { // 联通 HttpHost httpHost = new HttpHost("10.0.0.172", 80); customerHttpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, httpHost); } else if ("cmwap".equalsIgnoreCase(currentAPN)) { // 移动 HttpHost httpHost = new HttpHost("10.0.0.172", 80); customerHttpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, httpHost); } } else { // Log.i(TAG, "----------isWifiConnected-------- "); } } networkchanged = false; return customerHttpClient; }