Java Code Examples for android.net.NetworkInfo#getTypeName()
The following examples show how to use
android.net.NetworkInfo#getTypeName() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NetWorkUtils.java From YCAudioPlayer with Apache License 2.0 | 6 votes |
/** * 检查WIFI状态 */ public static boolean checkWifiState(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = null; if (manager != null) { networkInfo = manager.getActiveNetworkInfo(); } if (networkInfo != null && networkInfo.isConnected()) { String type = networkInfo.getTypeName(); if (type.equalsIgnoreCase("WIFI")) { return true; } else if (type.equalsIgnoreCase("MOBILE")) { return false; } } return false; }
Example 2
Source File: Tools.java From BigApp_Discuz_Android with Apache License 2.0 | 6 votes |
/** * 获取网络类型 * * @return */ public static String getNetworkType(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (manager == null) { return ""; } NetworkInfo networkInfo = manager.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { if (ConnectivityManager.TYPE_WIFI == networkInfo.getType()) { return networkInfo.getTypeName(); } return networkInfo.getExtraInfo(); } return "network unavailable"; }
Example 3
Source File: ImageLoaderUtil.java From LRecyclerView with Apache License 2.0 | 6 votes |
/** * get the network type (wifi,wap,2g,3g) * * @param context * @return */ public static int getNetWorkType(Context context) { int mNetWorkType = NETWORKTYPE_INVALID; 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")) { mNetWorkType = NETWORKTYPE_WIFI; } else if (type.equalsIgnoreCase("MOBILE")) { String proxyHost = android.net.Proxy.getDefaultHost(); mNetWorkType = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORKTYPE_3G : NETWORKTYPE_2G) : NETWORKTYPE_WAP; } } else { mNetWorkType = NETWORKTYPE_INVALID; } return mNetWorkType; }
Example 4
Source File: NetHelper.java From BaseProject with Apache License 2.0 | 6 votes |
/** * 获取网络类型[未经验证] * * @param context * @return */ public static int getNetWorkType(Context context) { /** wifi网络 */ final int NETWORKTYPE_WIFI = 1; /** 2G网络 */ final int NETWORKTYPE_2G = 2; /** 3G和3G以上网络,或统称为快速网络 */ final int NETWORKTYPE_3G = 3; int mNetWorkType = 0; 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")) { mNetWorkType = NETWORKTYPE_WIFI; } else if (type.equalsIgnoreCase("MOBILE")) { String proxyHost = android.net.Proxy.getDefaultHost(); mNetWorkType = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORKTYPE_3G : NETWORKTYPE_2G) : NETWORKTYPE_2G; } } return mNetWorkType; }
Example 5
Source File: LDNetUtil.java From AndroidHttpCapture with MIT License | 6 votes |
@SuppressWarnings({ "deprecation" }) public static String getNetWorkType(Context context) { String mNetWorkType = null; ConnectivityManager manager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (manager == null) { return "ConnectivityManager not found"; } NetworkInfo networkInfo = manager.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(); if (TextUtils.isEmpty(proxyHost)) { mNetWorkType = mobileNetworkType(context); } else { mNetWorkType = NETWORKTYPE_WAP; } } } else { mNetWorkType = NETWORKTYPE_INVALID; } return mNetWorkType; }
Example 6
Source File: AptoideUtils.java From aptoide-client-v8 with GNU General Public License v3.0 | 6 votes |
public static String getConnectionType(ConnectivityManager connectivityManager) { final ConnectivityManager manager = connectivityManager; final NetworkInfo info = manager.getActiveNetworkInfo(); if (info != null && info.getTypeName() != null) { switch (info.getType()) { case TYPE_ETHERNET: return "ethernet"; case TYPE_WIFI: return "wifi"; case TYPE_MOBILE: return "mobile"; } } return "unknown"; }
Example 7
Source File: ConfigsFragment.java From apigee-android-sdk with Apache License 2.0 | 6 votes |
public String activeNetworkStatus() { String status = "Not Connected"; if ( mainActivity.hadConnectivityOnStartup() ) { try { ConnectivityManager connectivityManager = (ConnectivityManager) mainActivity .getSystemService(Context.CONNECTIVITY_SERVICE); if( connectivityManager != null ) { NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo != null) { String networkTypeName = networkInfo.getTypeName(); if( networkTypeName != null ) { status = formatString(networkTypeName).toUpperCase(); } } } } catch (Exception e) { status = "Unknown"; } } return status; }
Example 8
Source File: NetUtils.java From Aria with Apache License 2.0 | 6 votes |
/** * 获取网络状态,wifi,wap,2g,3g. * * @param context 上下文 * @return int 网络状态 {@link #NETWORK_TYPE_2G},{@link #NETWORK_TYPE_3G}, * {@link #NETWORK_TYPE_INVALID},{@link #NETWORK_TYPE_WAP},{@link #NETWORK_TYPE_WIFI} */ public static int getNetWorkType(Context context) { int netWorkType = -1; 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")) { netWorkType = NETWORK_TYPE_WIFI; } else if (type.equalsIgnoreCase("MOBILE")) { String proxyHost = android.net.Proxy.getDefaultHost(); netWorkType = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORK_TYPE_3G : NETWORK_TYPE_2G) : NETWORK_TYPE_WAP; } } else { netWorkType = NETWORK_TYPE_INVALID; } return netWorkType; }
Example 9
Source File: Network.java From MiBandDecompiled with Apache License 2.0 | 6 votes |
public static String getActiveNetworkName(Context context) { ConnectivityManager connectivitymanager = (ConnectivityManager)context.getSystemService("connectivity"); if (connectivitymanager == null) { return "null"; } NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo(); if (networkinfo == null) { return "null"; } if (TextUtils.isEmpty(networkinfo.getSubtypeName())) { return networkinfo.getTypeName(); } else { Object aobj[] = new Object[2]; aobj[0] = networkinfo.getTypeName(); aobj[1] = networkinfo.getSubtypeName(); return String.format("%s-%s", aobj); } }
Example 10
Source File: c.java From MiBandDecompiled with Apache License 2.0 | 6 votes |
public String f() { if (d.checkCallingOrSelfPermission("android.permission.ACCESS_NETWORK_STATE") != 0) { return ""; } if (f == null) { return ""; } NetworkInfo networkinfo = f.getActiveNetworkInfo(); if (networkinfo == null) { return ""; } else { return networkinfo.getTypeName(); } }
Example 11
Source File: Network.java From MiBandDecompiled with Apache License 2.0 | 6 votes |
public static String getActiveNetworkName(Context context) { ConnectivityManager connectivitymanager = (ConnectivityManager)context.getSystemService("connectivity"); if (connectivitymanager == null) { return "null"; } NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo(); if (networkinfo == null) { return "null"; } if (TextUtils.isEmpty(networkinfo.getSubtypeName())) { return networkinfo.getTypeName(); } else { Object aobj[] = new Object[2]; aobj[0] = networkinfo.getTypeName(); aobj[1] = networkinfo.getSubtypeName(); return String.format("%s-%s", aobj); } }
Example 12
Source File: a.java From letv with Apache License 2.0 | 5 votes |
public static String e(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity"); if (connectivityManager == null) { return "MOBILE"; } NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetworkInfo != null) { return activeNetworkInfo.getTypeName(); } return "MOBILE"; }
Example 13
Source File: NetworkManager.java From jpHolo with MIT License | 5 votes |
/** * 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.toLowerCase().equals(CELLULAR)) { 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; }
Example 14
Source File: NetWorkUtils.java From FamilyChat with Apache License 2.0 | 5 votes |
/** * 获取当前当前网络类型名字 */ 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; }
Example 15
Source File: Network.java From batteryhub with Apache License 2.0 | 5 votes |
/** * Get the network type, for example Wifi, mobile, wimax, or none. */ public static String getType(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (manager == null) return TYPE_UNKNOWN; NetworkInfo info = manager.getActiveNetworkInfo(); return (info == null) ? TYPE_UNKNOWN : info.getTypeName(); }
Example 16
Source File: AppNetworkMgr.java From AndroidWallet with GNU General Public License v3.0 | 5 votes |
/** * Get network type name * * @param context context * @return NetworkTypeName */ 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; }
Example 17
Source File: NetworkConnectivityReceiver.java From Yahala-Messenger with MIT License | 4 votes |
@Override public void onReceive(Context context, Intent intent) { System.out.print("onReceive; intent=" + intent.getAction()); com.yahala.xmpp.Settings settings = com.yahala.xmpp.Settings.getInstance(); ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (debug) { for (NetworkInfo networkInfo : cm.getAllNetworkInfo()) log(networkInfo); } NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo(); if (activeNetworkInfo != null) { System.out.print("ActiveNetworkInfo follows:"); log(activeNetworkInfo); } boolean connected; boolean networkTypeChanged; String lastActiveNetworkType = settings.getLastActiveNetwork(); if (activeNetworkInfo != null) { // we have an active data connection String networkTypeName = activeNetworkInfo.getTypeName(); connected = true; networkTypeChanged = false; if (!networkTypeName.equals(lastActiveNetworkType)) { System.out.print("networkTypeChanged current=" + networkTypeName + " last=" + lastActiveNetworkType); settings.setLastActiveNetwork(networkTypeName); networkTypeChanged = true; } } else { // we have *no* active data connection connected = false; if (lastActiveNetworkType.length() != 0) { networkTypeChanged = true; } else { networkTypeChanged = false; } settings.setLastActiveNetwork(""); } /*LOG.d("Sending NETWORK_STATUS_CHANGED connected=" + connected + " changed=" + networkTypeChanged); Intent i = new Intent(context, TransportService.class); i.setAction(Constants.ACTION_NETWORK_STATUS_CHANGED); i.putExtra(Constants.EXTRA_NETWORK_TYPE_CHANGED, networkTypeChanged); i.putExtra(Constants.EXTRA_NETWORK_CONNECTED, connected); context.startService(i);*/ }
Example 18
Source File: DynamicReceiver4.java From CSipSimple with GNU General Public License v3.0 | 4 votes |
/** * Treat the fact that the connectivity has changed * @param info Network info * @param incomingOnly start only if for outgoing * @throws SameThreadException */ private void onConnectivityChanged(NetworkInfo info, boolean isSticky) throws SameThreadException { // We only care about the default network, and getActiveNetworkInfo() // is the only way to distinguish them. However, as broadcasts are // delivered asynchronously, we might miss DISCONNECTED events from // getActiveNetworkInfo(), which is critical to our SIP stack. To // solve this, if it is a DISCONNECTED event to our current network, // respect it. Otherwise get a new one from getActiveNetworkInfo(). if (info == null || info.isConnected() || !info.getTypeName().equals(mNetworkType)) { ConnectivityManager cm = (ConnectivityManager) service.getSystemService(Context.CONNECTIVITY_SERVICE); info = cm.getActiveNetworkInfo(); } boolean connected = (info != null && info.isConnected() && service.isConnectivityValid()); String networkType = connected ? info.getTypeName() : "null"; String currentRoutes = dumpRoutes(); String oldRoutes; synchronized (mRoutes) { oldRoutes = mRoutes; } // Ignore the event if the current active network is not changed. if (connected == mConnected && networkType.equals(mNetworkType) && currentRoutes.equals(oldRoutes)) { return; } if(Log.getLogLevel() >= 4) { if(!networkType.equals(mNetworkType)) { Log.d(THIS_FILE, "onConnectivityChanged(): " + mNetworkType + " -> " + networkType); }else { Log.d(THIS_FILE, "Route changed : "+ mRoutes+" -> "+currentRoutes); } } // Now process the event synchronized (mRoutes) { mRoutes = currentRoutes; } mConnected = connected; mNetworkType = networkType; if(!isSticky) { if (connected) { service.restartSipStack(); } else { Log.d(THIS_FILE, "We are not connected, stop"); if(service.stopSipStack()) { service.stopSelf(); } } } }
Example 19
Source File: NetworkConnectChangedReceiver.java From AndroidDemo with MIT License | 4 votes |
@Override public void onReceive(Context context, Intent intent) {// 这个监听wifi的打开与关闭,与wifi的连接无关 if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) { int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0); Log.e(TAG0, "WIFI状态:" + wifiState); switch (wifiState) { case WifiManager.WIFI_STATE_DISABLED: //APP.getInstance().setEnablaWifi(false); Log.e(TAG0,"WIFI_STATE_DISABLED" ); break; case WifiManager.WIFI_STATE_DISABLING: Log.e(TAG0,"WIFI_STATE_DISABLING" ); break; case WifiManager.WIFI_STATE_ENABLING: Log.e(TAG0,"WIFI_STATE_ENABLING" ); break; case WifiManager.WIFI_STATE_ENABLED: //APP.getInstance().setEnablaWifi(true); Log.e(TAG0,"WIFI_STATE_ENABLED"); break; case WifiManager.WIFI_STATE_UNKNOWN: Log.e(TAG0,"WIFI_STATE_UNKNOWN" ); break; default: break; } } // 这个监听wifi的连接状态即是否连上了一个有效无线路由,当上边广播的状态是WifiManager // .WIFI_STATE_DISABLING,和WIFI_STATE_DISABLED的时候,根本不会接到这个广播。 // 在上边广播接到广播是WifiManager.WIFI_STATE_ENABLED状态的同时也会接到这个广播, // 当然刚打开wifi肯定还没有连接到有效的无线 if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) { Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); if (null != parcelableExtra) { NetworkInfo networkInfo = (NetworkInfo) parcelableExtra; NetworkInfo.State state = networkInfo.getState(); boolean isConnected = state == NetworkInfo.State.CONNECTED;// 当然,这边可以更精确的确定状态 Log.e(TAG1, "WIFI连接状态:" + isConnected); if (isConnected) { //APP.getInstance().setWifi(true); } else { // APP.getInstance().setWifi(false); } } } // 这个监听网络连接的设置,包括wifi和移动数据的打开和关闭。. // 最好用的还是这个监听。wifi如果打开,关闭,以及连接上可用的连接都会接到监听。 // 这个广播的最大弊端是比上边两个广播的反应要慢,如果只是要监听wifi,觉得还是用上边两个配合比较合适。 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Log.i(TAG2, "CONNECTIVITY_ACTION"); string = "\n您的手机当前网络状态是:\n\n"; NetworkInfo activeNetwork = manager.getActiveNetworkInfo(); if (activeNetwork != null) { // connected to the internet if (activeNetwork.isConnected()) { if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) { // connected to wifi string += "当前WiFi连接可用 "; } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { // connected to the mobile provider's data plan string += "当前移动网络连接可用 "; } } else { string += "当前没有网络连接,请确保你已经打开网络 "; } string += "\n类型名:" + activeNetwork.getTypeName()+ "\n子类型名:" + activeNetwork.getSubtypeName()+ "\n状态:" + activeNetwork.getState()+ "\n详细状态:" + activeNetwork.getDetailedState().name()+ "\n额外状态:" + activeNetwork.getExtraInfo()+ "\n类型:" + activeNetwork.getType(); } else { // not connected to the internet string += "当前没有网络连接,请确保你已经打开网络 "; } Log.e("String:",string); brInteraction.setText(string); } }
Example 20
Source File: ApigeeActiveSettings.java From apigee-android-sdk with Apache License 2.0 | 4 votes |
private boolean matchesDeviceTypeFilter( ApigeeApp config) { if (config.getDeviceTypeOverrideEnabled()) { try { TelephonyManager telephonyManager = (TelephonyManager) appActivity .getSystemService(Context.TELEPHONY_SERVICE); ConnectivityManager connectivityManager = (ConnectivityManager) appActivity .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager .getActiveNetworkInfo(); String deviceModel = Build.MODEL; String devicePlatform = ApigeeMonitoringClient.getDevicePlatform(); String networkOperator = telephonyManager.getNetworkOperatorName(); String networkType = networkInfo.getTypeName(); if (findRegexMatch(config.getDeviceModelRegexFilters(), deviceModel)) { Log.v(ClientLog.TAG_MONITORING_CLIENT, "Found device model match"); return true; } if (findRegexMatch(config.getDevicePlatformRegexFilters(), devicePlatform)) { Log.v(ClientLog.TAG_MONITORING_CLIENT, "Found device platform match"); return true; } if (findRegexMatch(config.getNetworkOperatorRegexFilters(), networkOperator)) { Log.v(ClientLog.TAG_MONITORING_CLIENT, "Found network operator match"); return true; } if (findRegexMatch(config.getNetworkTypeRegexFilters(), networkType)) { Log.v(ClientLog.TAG_MONITORING_CLIENT, "Found network type match"); return true; } } catch (SecurityException e) { Log.w(ClientLog.TAG_MONITORING_CLIENT,"Security caught. AndroidManifest not configured to read phone state permissions : " + e.getMessage()); return false; } } return false; }