List of usage examples for android.net NetworkInfo getType
@Deprecated public int getType()
From source file:com.newsrob.EntryManager.java
boolean isOnWiFi() { if (connectivityManager == null) connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager == null) { PL.log("EntryManager. Wasn't able to get CONNECTIVITY_SERVICE.", ctx); WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (wifiInfo != null) PL.log("WiFi Info=" + wifiInfo + " SSID=" + wifiInfo.getSSID(), ctx); return false; }/* w ww . java 2 s . c om*/ final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo == null) { PL.log("EntryManager. Wasn't able to get Network Info.", ctx); return false; } if (ConnectivityManager.TYPE_WIFI != networkInfo.getType()) { PL.log("EntryManager. Network Info Type was not WiFi, but " + networkInfo.getType() + ".", ctx); return false; } return true; }
From source file:com.grazerss.EntryManager.java
boolean isOnWiFi() { if (connectivityManager == null) { connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); }//w w w . ja va2s . co m if (connectivityManager == null) { PL.log("EntryManager. Wasn't able to get CONNECTIVITY_SERVICE.", ctx); WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (wifiInfo != null) { PL.log("WiFi Info=" + wifiInfo + " SSID=" + wifiInfo.getSSID(), ctx); } return false; } final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo == null) { PL.log("EntryManager. Wasn't able to get Network Info.", ctx); return false; } if (ConnectivityManager.TYPE_WIFI != networkInfo.getType()) { PL.log("EntryManager. Network Info Type was not WiFi, but " + networkInfo.getType() + ".", ctx); return false; } return true; }
From source file:com.irccloud.android.NetworkConnection.java
public Bitmap fetchImage(URL url, boolean cacheOnly) throws Exception { HttpURLConnection conn = null; Proxy proxy = null;// w w w . j av a 2 s . co m String host = null; int port = -1; if (Build.VERSION.SDK_INT < 11) { Context ctx = IRCCloudApplication.getInstance().getApplicationContext(); if (ctx != null) { host = android.net.Proxy.getHost(ctx); port = android.net.Proxy.getPort(ctx); } } else { host = System.getProperty("http.proxyHost", null); try { port = Integer.parseInt(System.getProperty("http.proxyPort", "8080")); } catch (NumberFormatException e) { port = -1; } } if (host != null && host.length() > 0 && !host.equalsIgnoreCase("localhost") && !host.equalsIgnoreCase("127.0.0.1") && port > 0) { InetSocketAddress proxyAddr = new InetSocketAddress(host, port); proxy = new Proxy(Proxy.Type.HTTP, proxyAddr); } if (host != null && host.length() > 0 && !host.equalsIgnoreCase("localhost") && !host.equalsIgnoreCase("127.0.0.1") && port > 0) { Crashlytics.log(Log.DEBUG, TAG, "Requesting: " + url + " via proxy: " + host); } else { Crashlytics.log(Log.DEBUG, TAG, "Requesting: " + url); } if (url.getProtocol().toLowerCase().equals("https")) { HttpsURLConnection https = (HttpsURLConnection) ((proxy != null) ? url.openConnection(proxy) : url.openConnection(Proxy.NO_PROXY)); if (url.getHost().equals(IRCCLOUD_HOST)) https.setSSLSocketFactory(IRCCloudSocketFactory); conn = https; } else { conn = (HttpURLConnection) ((proxy != null) ? url.openConnection(proxy) : url.openConnection(Proxy.NO_PROXY)); } conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setUseCaches(true); conn.setRequestProperty("User-Agent", useragent); if (cacheOnly) conn.addRequestProperty("Cache-Control", "only-if-cached"); Bitmap bitmap = null; try { ConnectivityManager cm = (ConnectivityManager) IRCCloudApplication.getInstance() .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI) { Crashlytics.log(Log.DEBUG, TAG, "Loading via WiFi"); } else { Crashlytics.log(Log.DEBUG, TAG, "Loading via mobile"); } } catch (Exception e) { e.printStackTrace(); } try { if (conn.getInputStream() != null) { bitmap = BitmapFactory.decodeStream(conn.getInputStream()); } } catch (FileNotFoundException e) { return null; } catch (IOException e) { e.printStackTrace(); } conn.disconnect(); return bitmap; }
From source file:com.irccloud.android.NetworkConnection.java
public String fetch(URL url, String postdata, String sk, String token, HashMap<String, String> headers) throws Exception { HttpURLConnection conn = null; Proxy proxy = null;//ww w. ja va 2s. co m String host = null; int port = -1; if (Build.VERSION.SDK_INT < 11) { Context ctx = IRCCloudApplication.getInstance().getApplicationContext(); if (ctx != null) { host = android.net.Proxy.getHost(ctx); port = android.net.Proxy.getPort(ctx); } } else { host = System.getProperty("http.proxyHost", null); try { port = Integer.parseInt(System.getProperty("http.proxyPort", "8080")); } catch (NumberFormatException e) { port = -1; } } if (host != null && host.length() > 0 && !host.equalsIgnoreCase("localhost") && !host.equalsIgnoreCase("127.0.0.1") && port > 0) { InetSocketAddress proxyAddr = new InetSocketAddress(host, port); proxy = new Proxy(Proxy.Type.HTTP, proxyAddr); } if (host != null && host.length() > 0 && !host.equalsIgnoreCase("localhost") && !host.equalsIgnoreCase("127.0.0.1") && port > 0) { Crashlytics.log(Log.DEBUG, TAG, "Requesting: " + url + " via proxy: " + host); } else { Crashlytics.log(Log.DEBUG, TAG, "Requesting: " + url); } if (url.getProtocol().toLowerCase().equals("https")) { HttpsURLConnection https = (HttpsURLConnection) ((proxy != null) ? url.openConnection(proxy) : url.openConnection(Proxy.NO_PROXY)); if (url.getHost().equals(IRCCLOUD_HOST)) https.setSSLSocketFactory(IRCCloudSocketFactory); conn = https; } else { conn = (HttpURLConnection) ((proxy != null) ? url.openConnection(proxy) : url.openConnection(Proxy.NO_PROXY)); } conn.setConnectTimeout(5000); conn.setReadTimeout(5000); conn.setUseCaches(false); conn.setRequestProperty("User-Agent", useragent); conn.setRequestProperty("Accept", "application/json"); if (headers != null) { for (String key : headers.keySet()) { conn.setRequestProperty(key, headers.get(key)); } } if (sk != null) conn.setRequestProperty("Cookie", "session=" + sk); if (token != null) conn.setRequestProperty("x-auth-formtoken", token); if (postdata != null) { conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); OutputStream ostr = null; try { ostr = conn.getOutputStream(); ostr.write(postdata.getBytes()); } catch (Exception e) { e.printStackTrace(); } finally { if (ostr != null) ostr.close(); } } BufferedReader reader = null; String response = ""; try { ConnectivityManager cm = (ConnectivityManager) IRCCloudApplication.getInstance() .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI) { Crashlytics.log(Log.DEBUG, TAG, "Loading via WiFi"); } else { Crashlytics.log(Log.DEBUG, TAG, "Loading via mobile"); } } catch (Exception e) { } try { if (conn.getInputStream() != null) { reader = new BufferedReader(new InputStreamReader(conn.getInputStream()), 512); } } catch (IOException e) { if (conn.getErrorStream() != null) { reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()), 512); } } if (reader != null) { response = toString(reader); reader.close(); } conn.disconnect(); return response; }
From source file:de.vanita5.twittnuker.util.Utils.java
public static boolean isOnWifi(final Context context) { if (context == null) return false; final ConnectivityManager conn = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo networkInfo = conn.getActiveNetworkInfo(); return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI && networkInfo.isConnected(); }
From source file:com.ubiLive.GameCloud.Browser.WebBrowser.java
private void processNetworkType(Intent intent, Context context) { int netType;/*from www . j a va 2 s .c om*/ int netSubtype; int networkType; boolean isConnected; String reloadUrl; DebugLog.d(TAG, "processNetworkType() enter"); NetworkInfo netInfo = Utils.getCurNetworkInfo(context); if (netInfo == null || netInfo.isAvailable() == false) { DebugLog.d(TAG, "getCurNetworkInfo() is null"); netInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); } if (netInfo == null || netInfo.isAvailable() == false) { DebugLog.d(TAG, "getCurNetworkInfo() netInfo is null line1383"); if (mbNetworIsConnect != false) { mbNetworIsConnect = false; reloadUrl = GameInfo.getErrorUrl(); reload(reloadUrl); } Utils.setNetworkType(Constants.NETWORKTYPE_NONE); return; } netType = netInfo.getType(); netSubtype = netInfo.getSubtype(); isConnected = netInfo.isConnected(); DebugLog.e(TAG, "======processNetworkType nettype = " + netType + ",netSubtype = " + netSubtype + ",isConnect = " + isConnected); switch (netType) { case ConnectivityManager.TYPE_MOBILE_SUPL: case ConnectivityManager.TYPE_MOBILE_MMS: case ConnectivityManager.TYPE_MOBILE_HIPRI: case ConnectivityManager.TYPE_MOBILE_DUN: case ConnectivityManager.TYPE_MOBILE: DebugLog.d(TAG, "processNetworkType ConnectivityManager.TYPE_MOBILE netSubtype=" + netSubtype); if (netSubtype == TelephonyManager.NETWORK_TYPE_LTE) {//4 DebugLog.d(TAG, "processNetworkType ConnectivityManager.TYPE_MOBILE 4G"); networkType = Constants.NETWORKTYPE_LTE; } else {//3 DebugLog.d(TAG, "processNetworkType ConnectivityManager.TYPE_MOBILE 3G"); networkType = Constants.NETWORKTYPE_3G; } break; case ConnectivityManager.TYPE_WIFI: DebugLog.d(TAG, "processNetworkType ConnectivityManager.TYPE_WIFI"); networkType = Constants.NETWORKTYPE_WIFI; break; case ConnectivityManager.TYPE_ETHERNET: DebugLog.d(TAG, "processNetworkType ConnectivityManager.TYPE_ETHERNET"); networkType = Constants.NETWORKTYPE_ETHERNET; break; default: DebugLog.d(TAG, "other network status"); networkType = Constants.NETWORKTYPE_OTHERS; break; } DebugLog.e(TAG, "======processNetworkType nettype = " + netType + ",netSubtype = " + netSubtype + ",isConnect = " + isConnected + ",mbNetworIsConnect = " + mbNetworIsConnect); if (isConnected) { reloadUrl = Utils.checkUrl(GameInfo.DEFAULT_URL); Utils.setNetworkType(networkType); // Utils.setNetworkType(Constants.NETWORKTYPE_WIFI);//temp change } else { reloadUrl = GameInfo.getErrorUrl(); Utils.setNetworkType(Constants.NETWORKTYPE_NONE); } // if((networkType == Constants.NETWORKTYPE_WIFI || networkType == Constants.NETWORKTYPE_LTE) && mbNetworIsConnect != isConnected){ if (mbNetworIsConnect != isConnected) { mbNetworIsConnect = isConnected; reload(reloadUrl); DebugLog.d(TAG, "Notify reload finish"); } }
From source file:com.codename1.impl.android.AndroidImplementation.java
/** * @inheritDoc/* w w w .j a va2s . co m*/ */ public int getAPType(String id) { if (apIds == null) { getAPIds(); } NetworkInfo info = (NetworkInfo) apIds.get(id); if (info == null) { return NetworkManager.ACCESS_POINT_TYPE_UNKNOWN; } int type = info.getType(); int subType = info.getSubtype(); if (type == ConnectivityManager.TYPE_WIFI) { return NetworkManager.ACCESS_POINT_TYPE_WLAN; } else if (type == ConnectivityManager.TYPE_MOBILE) { switch (subType) { case TelephonyManager.NETWORK_TYPE_1xRTT: return NetworkManager.ACCESS_POINT_TYPE_NETWORK2G; // ~ 50-100 kbps case TelephonyManager.NETWORK_TYPE_CDMA: return NetworkManager.ACCESS_POINT_TYPE_NETWORK2G; // ~ 14-64 kbps case TelephonyManager.NETWORK_TYPE_EDGE: return NetworkManager.ACCESS_POINT_TYPE_NETWORK2G; // ~ 50-100 kbps case TelephonyManager.NETWORK_TYPE_EVDO_0: return NetworkManager.ACCESS_POINT_TYPE_NETWORK3G; // ~ 400-1000 kbps case TelephonyManager.NETWORK_TYPE_EVDO_A: return NetworkManager.ACCESS_POINT_TYPE_NETWORK3G; // ~ 600-1400 kbps case TelephonyManager.NETWORK_TYPE_GPRS: return NetworkManager.ACCESS_POINT_TYPE_NETWORK2G; // ~ 100 kbps case TelephonyManager.NETWORK_TYPE_HSDPA: return NetworkManager.ACCESS_POINT_TYPE_NETWORK3G; // ~ 2-14 Mbps case TelephonyManager.NETWORK_TYPE_HSPA: return NetworkManager.ACCESS_POINT_TYPE_NETWORK3G; // ~ 700-1700 kbps case TelephonyManager.NETWORK_TYPE_HSUPA: return NetworkManager.ACCESS_POINT_TYPE_NETWORK3G; // ~ 1-23 Mbps case TelephonyManager.NETWORK_TYPE_UMTS: return NetworkManager.ACCESS_POINT_TYPE_NETWORK3G; // ~ 400-7000 kbps /* * Above API level 7, make sure to set android:targetSdkVersion * to appropriate level to use these */ case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11 return NetworkManager.ACCESS_POINT_TYPE_NETWORK3G; // ~ 1-2 Mbps case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9 return NetworkManager.ACCESS_POINT_TYPE_NETWORK3G; // ~ 5 Mbps case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13 return NetworkManager.ACCESS_POINT_TYPE_NETWORK3G; // ~ 10-20 Mbps case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8 return NetworkManager.ACCESS_POINT_TYPE_NETWORK2G; // ~25 kbps case TelephonyManager.NETWORK_TYPE_LTE: // API level 11 return NetworkManager.ACCESS_POINT_TYPE_NETWORK3G; // ~ 10+ Mbps // Unknown case TelephonyManager.NETWORK_TYPE_UNKNOWN: default: return NetworkManager.ACCESS_POINT_TYPE_NETWORK2G; } } else { return NetworkManager.ACCESS_POINT_TYPE_UNKNOWN; } }
From source file:com.codename1.impl.android.AndroidImplementation.java
/** * @inheritDoc//from ww w . ja v a 2 s.c o m */ public void setCurrentAccessPoint(String id) { if (apIds == null) { getAPIds(); } NetworkInfo info = (NetworkInfo) apIds.get(id); if (info == null || info.isConnectedOrConnecting()) { return; } ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE); cm.setNetworkPreference(info.getType()); }