Guest User

Untitled

a guest
Oct 23rd, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. public class NetworkUtils {
  2.  
  3.  
  4. public static NetworkInfo getNetworkInfo(final Context context) {
  5. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  6. return cm.getActiveNetworkInfo();
  7. }
  8.  
  9. public static boolean isConnected(final Context context) {
  10. NetworkInfo info = getNetworkInfo(context);
  11. return (info != null && info.isConnected());
  12. }
  13.  
  14. public static boolean isConnectedWifi(final Context context) {
  15. NetworkInfo info = getNetworkInfo(context);
  16. return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
  17. }
  18.  
  19. public static boolean isConnectedMobile(final Context context) {
  20. NetworkInfo info = getNetworkInfo(context);
  21. return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
  22. }
  23.  
  24. public static boolean isConnectedFast(final Context context) {
  25. NetworkInfo info = getNetworkInfo(context);
  26. return (info != null && info.isConnected() && isConnectionFast(info.getType(), info.getSubtype()));
  27. }
  28.  
  29. public static boolean isConnectionFast(int type, int subType) {
  30. if (type == ConnectivityManager.TYPE_WIFI) {
  31. return true;
  32. } else if (type == ConnectivityManager.TYPE_MOBILE) {
  33. switch (subType) {
  34. case TelephonyManager.NETWORK_TYPE_1xRTT:
  35. return false; // ~ 50-100 kbps
  36. case TelephonyManager.NETWORK_TYPE_CDMA:
  37. return false; // ~ 14-64 kbps
  38. case TelephonyManager.NETWORK_TYPE_EDGE:
  39. return false; // ~ 50-100 kbps
  40. case TelephonyManager.NETWORK_TYPE_EVDO_0:
  41. return true; // ~ 400-1000 kbps
  42. case TelephonyManager.NETWORK_TYPE_EVDO_A:
  43. return true; // ~ 600-1400 kbps
  44. case TelephonyManager.NETWORK_TYPE_GPRS:
  45. return false; // ~ 100 kbps
  46. case TelephonyManager.NETWORK_TYPE_HSDPA:
  47. return true; // ~ 2-14 Mbps
  48. case TelephonyManager.NETWORK_TYPE_HSPA:
  49. return true; // ~ 700-1700 kbps
  50. case TelephonyManager.NETWORK_TYPE_HSUPA:
  51. return true; // ~ 1-23 Mbps
  52. case TelephonyManager.NETWORK_TYPE_UMTS:
  53. return true; // ~ 400-7000 kbps
  54. /*
  55. * Above API level 7, make sure to set android:targetSdkVersion
  56. * to appropriate level to use these
  57. */
  58. case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
  59. return true; // ~ 1-2 Mbps
  60. case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
  61. return true; // ~ 5 Mbps
  62. case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
  63. return true; // ~ 10-20 Mbps
  64. case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
  65. return false; // ~25 kbps
  66. case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
  67. return true; // ~ 10+ Mbps
  68. // Unknown
  69. case TelephonyManager.NETWORK_TYPE_UNKNOWN:
  70. default:
  71. return false;
  72. }
  73. } else {
  74. return false;
  75. }
  76. }
  77.  
  78.  
  79. }
Add Comment
Please, Sign In to add comment