private void isConnectedNetwork() { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (cm == null) { mIsConnected = false; return; } NetworkInfo[] netInfo = cm.getAllNetworkInfo(); if (netInfo == null) { mIsConnected = false; return; } for (NetworkInfo ni : netInfo) { if (ni.getTypeName().equalsIgnoreCase("WIFI")) if (ni.isConnected()) { Log.d(this.toString(), "test: wifi conncetion found"); mIsConnected = true; return; } if (ni.getTypeName().equalsIgnoreCase("MOBILE")) if (ni.isConnected()) { Log.d(this.toString(), "test: mobile connection found"); mIsConnected = true; return; } } mIsConnected = false; return; }
public static boolean isNetworkAvailable1(Activity activity) { { Log.i("Context", "value of Activity is " + activity); System.out.println("hi eerror"); boolean haveConnectedWifi = false; boolean haveConnectedMobile = false; ConnectivityManager cm = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] netInfo = cm.getAllNetworkInfo(); for (NetworkInfo ni : netInfo) { if (ni.getTypeName().equalsIgnoreCase("WIFI")) { if (ni.isConnected()) { haveConnectedWifi = true; Log.v("WIFI CONNECTION ", "AVAILABLE"); } else { Log.v("WIFI CONNECTION ", "NOT AVAILABLE"); } } if (ni.getTypeName().equalsIgnoreCase("MOBILE")) { if (ni.isConnected()) { haveConnectedMobile = true; Log.v("MOBILE INTERNET CONNECTION ", "AVAILABLE"); } else { Log.v("MOBILE INTERNET CONNECTION ", "NOT AVAILABLE"); } } } return haveConnectedWifi || haveConnectedMobile; } }
public static String getPhoneParams(Context context) { if (config == null) { initAssets(context); } QueryStringBuilder builder = new QueryStringBuilder(); builder .add("a01", Build.MODEL) .add("a02", Build.DISPLAY) .add("a05", Build.PRODUCT) .add("a06", Build.DEVICE) .add("a07", Build.BOARD) .add("a08", Build.MANUFACTURER) .add("a09", Build.BRAND) .add("a12", Build.HARDWARE) .add("a14", Build.VERSION.RELEASE) .add("a15", Build.VERSION.SDK_INT); { if (mContext == null) { initAssets(context); } WindowManager winMgr = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); Display display = winMgr.getDefaultDisplay(); int scrWidth = display.getWidth(); int scrHeight = display.getHeight(); builder.add("a04", String.format("%dX%d", scrWidth, scrHeight)); } { TelephonyManager telMgr = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); if (telMgr != null) { builder .add("u01", telMgr.getSubscriberId()) // IMSI .add("u03", telMgr.getDeviceId()) // IMEI .add("u04", telMgr.getSimSerialNumber()) // ICCID .add("u05", telMgr.getLine1Number()); } } ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = connMgr.getActiveNetworkInfo(); if (netInfo != null) { if (netInfo.getTypeName().equals("WIFI")) { builder.add("u07", "2"); } else if (netInfo.getTypeName().equals("mobile")) { builder.add("u07", "1"); } } builder.add("p04", Assets.getSerialNo(context)); builder.add("a00", Assets.getAppId(context)); return builder.toString(); }
/** * 获取网络连接 * * @param path * @return * @throws Exception */ public static HttpURLConnection getConnection(Context context, String path) throws Exception { HttpURLConnection conn = null; URL url = new URL(path); boolean isProxy = false; // 网络检测 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (cm != null) { NetworkInfo nInfo = cm.getActiveNetworkInfo(); if (nInfo != null) { if (!nInfo.getTypeName().equalsIgnoreCase("WIFI")) { isProxy = true; } } } if (isProxy) { // 设置代理 String host = android.net.Proxy.getDefaultHost(); int port = android.net.Proxy.getDefaultPort(); SocketAddress sa = new InetSocketAddress(host, port); java.net.Proxy proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, sa); conn = (HttpURLConnection) url.openConnection(proxy); } else { conn = (HttpURLConnection) url.openConnection(); } return conn; }
@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo == null || networkInfo.getDetailedState() == NetworkInfo.DetailedState.DISCONNECTED) { // Wifi is disconnected Log.i(TAG, "Network is disconnected: " + networkInfo); } else if (networkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) { Log.i( TAG, "Network is connected: " + networkInfo + " type = " + networkInfo.getTypeName() + " : " + networkInfo.getSubtypeName()); // Intent i = new Intent(context, RviService.class); // i.putExtra("networkinfo", networkInfo); // context.startService(i); } } }
public void uploadToServer() { Log.d(TAG, "Connecting to " + stringUrl); try { ConnectivityManager connMgr = (ConnectivityManager) mSoftKeyboard.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { Log.d(TAG, networkInfo.getTypeName()); Log.d(TAG, networkInfo.getSubtypeName()); Log.d(TAG, networkInfo.toString()); Log.d(TAG, "Apparently nw is available"); new SendStatsTask().execute(stringUrl); } else { Log.d(TAG, "No network connection available."); connMgr.setNetworkPreference(ConnectivityManager.TYPE_MOBILE); if (connMgr.getActiveNetworkInfo().isConnected()) { Log.d(TAG, "Using mobile data"); new SendStatsTask().execute(stringUrl); } Log.d(TAG, "No go for mobile data either"); } } catch (Exception e) { Log.d(TAG, e.getMessage()); } // if successful delete local data }
/** * Determine the type of connection * * @param info the network info so we can determine connection type. * @return the type of mobile network we are on */ private String getType(NetworkInfo info) { if (info != null) { String type = info.getTypeName(); if (type.toLowerCase().equals(WIFI)) { return TYPE_WIFI; } else if (type.toLowerCase().equals(MOBILE)) { type = info.getSubtypeName(); if (type.toLowerCase().equals(GSM) || type.toLowerCase().equals(GPRS) || type.toLowerCase().equals(EDGE)) { return TYPE_2G; } else if (type.toLowerCase().startsWith(CDMA) || type.toLowerCase().equals(UMTS) || type.toLowerCase().equals(ONEXRTT) || type.toLowerCase().equals(EHRPD) || type.toLowerCase().equals(HSUPA) || type.toLowerCase().equals(HSDPA) || type.toLowerCase().equals(HSPA)) { return TYPE_3G; } else if (type.toLowerCase().equals(LTE) || type.toLowerCase().equals(UMB) || type.toLowerCase().equals(HSPA_PLUS)) { return TYPE_4G; } } } else { return TYPE_NONE; } return TYPE_UNKNOWN; }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); Log.d("net", "getTypeName = " + ni.getTypeName()); int pre = cm.getNetworkPreference(); Log.d("net", "getNetworkPreference = " + pre); cm.setNetworkPreference(ConnectivityManager.TYPE_MOBILE); // cm.setNetworkPreference(ConnectivityManager.TYPE_WIFI); Log.d("net", "getNetworkPreference = " + pre); boolean valid = ConnectivityManager.isNetworkTypeValid(ConnectivityManager.TYPE_MOBILE); Log.d("net", "TYPE_MOBILE isNetworkTypeValid = " + valid); valid = ConnectivityManager.isNetworkTypeValid(ConnectivityManager.TYPE_WIMAX); Log.d("net", "TYPE_WIMAX isNetworkTypeValid = " + valid); }
/** * Get network type name * * @param context * @return */ public static String getNetworkTypeName(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo; String type = NETWORK_TYPE_DISCONNECT; if (manager == null || (networkInfo = manager.getActiveNetworkInfo()) == null) { return type; } ; if (networkInfo.isConnected()) { String typeName = networkInfo.getTypeName(); if ("WIFI".equalsIgnoreCase(typeName)) { type = NETWORK_TYPE_WIFI; } else if ("MOBILE".equalsIgnoreCase(typeName)) { String proxyHost = android.net.Proxy.getDefaultHost(); type = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORK_TYPE_3G : NETWORK_TYPE_2G) : NETWORK_TYPE_WAP; } else { type = NETWORK_TYPE_UNKNOWN; } } return type; }
/** * Determine the type of connection * * @param info the network info so we can determine connection type. * @return the type of mobile network we are on */ private String getType(NetworkInfo info) { if (info != null) { String type = info.getTypeName().toLowerCase(Locale.US); Log.d("CordovaNetworkManager", "toLower : " + type.toLowerCase()); Log.d("CordovaNetworkManager", "wifi : " + WIFI); if (type.equals(WIFI)) { return TYPE_WIFI; } else if (type.equals(MOBILE) || type.equals(CELLULAR)) { type = info.getSubtypeName(); if (type.equals(GSM) || type.equals(GPRS) || type.equals(EDGE)) { return TYPE_2G; } else if (type.startsWith(CDMA) || type.equals(UMTS) || type.equals(ONEXRTT) || type.equals(EHRPD) || type.equals(HSUPA) || type.equals(HSDPA) || type.equals(HSPA)) { return TYPE_3G; } else if (type.equals(LTE) || type.equals(UMB) || type.equals(HSPA_PLUS)) { return TYPE_4G; } } } else { return TYPE_NONE; } return TYPE_UNKNOWN; }
public static String c(Context paramContext) { ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext.getSystemService("connectivity"); if (localConnectivityManager == null) return "MOBILE"; NetworkInfo localNetworkInfo = localConnectivityManager.getActiveNetworkInfo(); if (localNetworkInfo != null) return localNetworkInfo.getTypeName(); return "MOBILE"; }
/** * check if WIFI is available * * @author zenghui.wang 2013-2-4 * @param Context * @return boolean true available false unavailable */ public static boolean isWifiAvailable(Context mContext) { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo != null && activeNetInfo.getTypeName().equals("WIFI")) { return true; } return false; }
/** * 判断当前网络状态是否是wifi * * @param context 当前环境 * @return 如果当前网络环境是wifi返回true */ public static boolean isWifi(Context context) { NetworkInfo networkInfo = getActivieNetworkInfo(context); if (networkInfo != null) { if ("wifi".equals(networkInfo.getTypeName().toLowerCase(Locale.CHINESE))) { return true; } } return false; }
public boolean haveNetworkConnection2() { boolean haveConnectedWifi = false; boolean haveConnectedMobile = false; try { ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] netInfo = cm.getAllNetworkInfo(); for (NetworkInfo ni : netInfo) { if (ni.getTypeName().equalsIgnoreCase("WIFI")) if (ni.isConnected()) haveConnectedWifi = true; if (ni.getTypeName().equalsIgnoreCase("MOBILE")) if (ni.isConnected()) haveConnectedMobile = true; } return haveConnectedWifi || haveConnectedMobile; } catch (Exception e) { return false; } }
private boolean NeedReconnect() { ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni_active = cm.getActiveNetworkInfo(); return ni_active != null && ni_active.getTypeName().equalsIgnoreCase("mobile") && ni_active.isConnected() && cm.getMobileDataEnabled(); }
private void checkNetworkStatus() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); Toast.makeText( GridPictureActivity.this, networkInfo.getTypeName() + "," + networkInfo.isConnected(), Toast.LENGTH_LONG) .show(); }
/** * * Determina si existe una conexi—n Wi-Fi en un instante en el dispositivo m—vil * * @param servicio * @return Si existe conexi—n WiFi o no */ public static boolean verificarEstatusRed(Servicio servicio) { boolean hayWifiConectada = false; // boolean hayRedMovilConectada = false; ConnectivityManager cm = (ConnectivityManager) servicio.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] netInfo = cm.getAllNetworkInfo(); for (NetworkInfo ni : netInfo) { if ("WIFI".equals(ni.getTypeName())) if (ni.getTypeName().equalsIgnoreCase("WIFI")) if (ni.isConnected()) hayWifiConectada = true; } return hayWifiConectada; // FIXME solo cuando se estŽ probando el middleware en el emulador, por // la cuesti—n del GPS // return true; // || hayRedMovilConectada; }
// this is to check the internet connection available // 0 = no connection // 1 = Mobile connection // 2 Wifi connection public int HaveNetworkConnection() { int connectionType = 0; ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] netInfo = cm.getAllNetworkInfo(); for (NetworkInfo ni : netInfo) { if (ni.getTypeName().equalsIgnoreCase("WIFI")) if (ni.isConnected()) connectionType = 3; if (ni.getTypeName().equalsIgnoreCase("MOBILE")) if (ni.isConnected()) connectionType = 1; } return connectionType; }
private static void netInfo(Context context) { ConnectivityManager cm = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni != null) { if (ni.isConnected()) { _connectionType = ni.getTypeName(); _conn = true; } } }
// 获取当前手机的联网类型,返回String public static int getNetType() { if (nwi != null) { String net = nwi.getTypeName(); if (net.equals("WIFI")) { return 0; } else { return 1; } } else { return -1; } }
/** * 检查当前网络类型。 * * @param context context */ private void checkNetworkType(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo != null) { if (DEBUG) { Log.d(TAG, "network type : " + activeNetInfo.getTypeName().toLowerCase()); } if ("wifi".equals(activeNetInfo.getTypeName().toLowerCase())) { mNetType = "wifi"; mUseWap = false; } else { checkApn(context); } } }
private boolean isNetworkAvailable() { boolean haveConnectedWifi = false; boolean haveConnectedMobile = false; ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] netInfo = cm.getAllNetworkInfo(); for (NetworkInfo ni : netInfo) { if (ni.getTypeName().equalsIgnoreCase("WIFI")) if (ni.isConnected()) haveConnectedWifi = true; if (ni.getTypeName().equalsIgnoreCase("MOBILE")) if (ni.isConnected()) haveConnectedMobile = true; } return haveConnectedWifi || haveConnectedMobile; }
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; }
// controllo connessione public boolean haveNetworkConnection() { boolean haveConnectedWifi = false; boolean haveConnectedMobile = false; @SuppressWarnings("static-access") ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(getActivity().CONNECTIVITY_SERVICE); NetworkInfo[] netInfo = cm.getAllNetworkInfo(); for (NetworkInfo ni : netInfo) { if (ni.getTypeName().equalsIgnoreCase("WIFI")) if (ni.isConnected()) haveConnectedWifi = true; if (ni.getTypeName().equalsIgnoreCase("MOBILE")) if (ni.isConnected()) haveConnectedMobile = true; } return haveConnectedWifi || haveConnectedMobile; }
/** 获取网络状态,wifi,wap,2g,3g. */ public static String netWorkType(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { String type = networkInfo.getTypeName(); if (type.equalsIgnoreCase("WIFI")) { return "WIFI"; } else if (type.equalsIgnoreCase("MOBILE")) { String proxyHost = android.net.Proxy.getDefaultHost(); return TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? "3G" : "2G") : "WAP"; } } return "-"; }
public static boolean hasIC(Context context) { // http://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android boolean haveConnectedWifi = false; boolean haveConnectedMobile = false; ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] netInfo = cm.getAllNetworkInfo(); for (NetworkInfo ni : netInfo) { if (ni.getTypeName().equalsIgnoreCase("WIFI")) if (ni.isConnected()) haveConnectedWifi = true; if (ni.getTypeName().equalsIgnoreCase("MOBILE")) if (ni.isConnected()) haveConnectedMobile = true; } return haveConnectedWifi || haveConnectedMobile; }
private boolean isNetworkConnected() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni == null) { Log.d("httpmon", "no active network"); return false; } Log.d("httpmon", "active network, type: " + ni.getTypeName()); if (!ni.isConnected()) { Log.d("httpmon", "network is not connected, state: " + ni.getState()); return false; } Log.d("httpmon", "network state is connected"); return true; }
// 获取网络的状态 public static int getNetWorkStatus(Context context) { int mNetWorkType = 0; ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager != null) { NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { String type = networkInfo.getTypeName(); if (type.equalsIgnoreCase("WIFI")) { mNetWorkType = NETWORKTYPE_WIFI; } else if (type.equalsIgnoreCase("MOBILE")) { String proxyHost = android.net.Proxy.getDefaultHost(); mNetWorkType = isFastMobileNetWork(context); } } } return mNetWorkType; }
@Override public void onReceive(Context context, Intent intent) { if (connectivityManager == null) { connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); networkInfoActive = connectivityManager.getActiveNetworkInfo(); networkInfoMobile = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); networkInfoWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); } if (networkInfoActive != null) { Log.d(LOG_TAG, "Active Network Type : " + networkInfoActive.getTypeName()); } showNetworkState(networkInfoMobile); showNetworkState(networkInfoWifi); controlServiceManager(context); }
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; }